Esempio n. 1
0
        static void Main(string[] args)
        {
            CurrentProcessPath = Process.GetCurrentProcess().MainModule.FileName;

            System.Diagnostics.Trace.WriteLine(CurrentProcessPath);
            if (args.Length == 1)
            {
                if (args[0].ToLower() == "start")
                {
                    bool createdNew;
                    System.Threading.Mutex instance = new System.Threading.Mutex(true, MutexName, out createdNew);
                    if (createdNew)
                    {
                        TraceHelper.TraceInfo("start mutex in");
                        ServiceMonitor curInstance = new ServiceMonitor();
                        curInstance.ExitFlag = false;
                        Application.AddMessageFilter(new MsgFilter(curInstance));
                        curInstance.Start();

                        //维持循环,接收推出消息
                        while (!curInstance.ExitFlag)
                        {
                            Application.DoEvents();
                            System.Threading.Thread.Sleep(1000);
                        }

                        TraceHelper.TraceInfo("ReleaseMutex");
                        instance.ReleaseMutex();
                    }
                    else
                    {
                        TraceHelper.TraceInfo("start mutex not  in");
                    }
                }

                if (args[0].ToLower() == "stop")
                {
                    bool createdNew;
                    System.Threading.Mutex instance = new System.Threading.Mutex(true, MutexName, out createdNew);
                    if (createdNew)
                    {
                        TraceHelper.TraceInfo("stop mutex   in");
                        instance.ReleaseMutex();
                    }
                    else
                    {
                        TraceHelper.TraceInfo("stop mutex  not in");
                        Process[] curProcessArray = Process.GetProcesses();

                        foreach (Process curProcess in curProcessArray)
                        {
                            try
                            {
                                if (curProcess.MainModule.FileName == CurrentProcessPath)
                                {
                                    System.Diagnostics.Trace.WriteLine("post stop message " + curProcess.Id);
                                    PostThreadMessage(curProcess.Threads[0].Id, (uint)MessageCode_Stop, IntPtr.Zero, IntPtr.Zero);
                                }
                            }
                            catch
                            {
                            }
                        }
                        TraceHelper.TraceInfo("exit");
                        Application.Exit();
                    }
                }
            }
        }
Esempio n. 2
0
 public MsgFilter(ServiceMonitor I_Instance)
 {
     curInstance = I_Instance;
 }