private static void MonitorProcessWorker(object Args)
        {
            ProcessMonitorManager appManager = (ProcessMonitorManager)Args;
            MainForm CurrentForm             = appManager._mainForm;

            appManager._mainForm.PrintAndWriteFileWithTime("Entry MonitorProcessWorker Thread.");

            while (true == appManager._runningMonitorThread)
            {
                try
                {
                    appManager._runningMonitorProcess = false;

                    Process[] Processlist = Process.GetProcesses();
                    foreach (Process CurProcess in Processlist)
                    {
                        // FullPath로 확인한다. 이름이 같아도 FullPath가 다르면 별개의 프로세스로 판단한다.
                        if (true == appManager._existPrevMonitorProcess &&
                            CurProcess.ProcessName == appManager.TargetProcessInfo.Name)
                        {
                            string curProcessFullPath = CurProcess.Modules[0].FileName;
                            if (0 == string.Compare(curProcessFullPath, appManager.TargetProcessInfo.FullPath))
                            {
                                TargetProcess = CurProcess;
                                appManager._runningMonitorProcess = true;
                                appManager.SaveMonitorProcessInfo(TargetProcess);

                                break;
                            }
                        }
                        else
                        {
                            if (CurProcess.ProcessName == appManager.TargetProcessInfo.Name)
                            {
                                string curProcessFullPath = CurProcess.Modules[0].FileName;
                                if (0 == string.Compare(curProcessFullPath, appManager.TargetProcessInfo.FullPath))
                                {
                                    // Running Process is true.
                                    TargetProcess = CurProcess;
                                    appManager._runningMonitorProcess = true;

                                    appManager.SaveMonitorProcessInfo(TargetProcess);
                                    break;
                                }
                            }
                        }
                    }

                    if (true == appManager._runningMonitorProcess)
                    {
                        string InfoText = String.Format("Process {0} is Running!!!", appManager.TargetProcessInfo.Name);
                    }
                    else if (false == appManager._runningMonitorProcess)
                    {
                        string strText = String.Format("Process {0} is not Running. now Try Running Process!!!!",
                                                       appManager.TargetProcessInfo.Name);

                        CurrentForm.PrintAndWriteFileWithTime(strText);

                        // Force Running Process.
                        strText = String.Format("Start Running Process!! [{0}], {1}]",
                                                appManager.TargetProcessInfo.FullPath, appManager.TargetProcessInfo.Name);

                        CurrentForm.PrintAndWriteFileWithTime(strText);

                        string titleBarText = string.Format("{0}", appManager.TargetProcessInfo.FullPath);
                        CurrentForm.AppendToMainTitleBarText(ref titleBarText);

                        string WorkingDirectory = System.IO.Directory.GetCurrentDirectory();

                        string PathOnly = AprilUtility.RemoveExeFileName(appManager.TargetProcessInfo.FullPath,
                                                                         appManager.TargetProcessInfo.Name);

                        if ("" != PathOnly)
                        {
                            System.IO.Directory.SetCurrentDirectory(PathOnly);
                            Process MonitorProcess = Process.Start(appManager.TargetProcessInfo.FullPath);

                            if (true == Properties.Settings.Default.IsSendMail)
                            {
                                if (true == System.IO.Directory.Exists(Properties.Settings.Default.SendMailDir))
                                {
                                    CurrentForm.PrintAndWriteFileWithTime("Send mail to Developer's Using python");

                                    // 서버가 죽었다고 메일을 보낸다. sendmail.py를 활용한다.
                                    string mailSubject = string.Format("\"Crash Server Process {0}({1})\"",
                                                                       appManager.TargetProcessInfo.FullPath,
                                                                       appManager.TargetProcessInfo.Name);


                                    string sendMailProcessName = "C:\\Python27\\python.exe";

                                    ProcessStartInfo pythonProcessInfo = new ProcessStartInfo();
                                    pythonProcessInfo.CreateNoWindow  = true;
                                    pythonProcessInfo.UseShellExecute = false;
                                    pythonProcessInfo.FileName        = sendMailProcessName;
                                    pythonProcessInfo.WindowStyle     = ProcessWindowStyle.Hidden;
                                    pythonProcessInfo.Arguments       = "SendMail.py " + mailSubject;
                                    using (Process SendMailProcess = Process.Start(pythonProcessInfo))
                                    {
                                        SendMailProcess.WaitForExit();
                                    }

                                    CurrentForm.PrintAndWriteFileWithTime("Complete to Send ");
                                }
                            }

                            System.IO.Directory.SetCurrentDirectory(WorkingDirectory);
                            if (MonitorProcess.ProcessName == appManager.TargetProcessInfo.Name)
                            {
                                appManager.SaveMonitorProcessInfo(MonitorProcess);
                                strText = String.Format("Success to Run Process [{0}]", appManager.TargetProcessInfo.Name);
                                CurrentForm.PrintAndWriteFileWithTime(strText);
                            }
                        }
                        else
                        {
                            strText = String.Format("Not Exist FullPath Directory [{0}]", appManager.TargetProcessInfo.Name);
                            CurrentForm.PrintAndWriteFileWithTime(strText);
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    CurrentForm.PrintAndWriteFileWithTime(ex.Message);
                }

                Thread.Sleep(2000);
            }
        }
Esempio n. 2
0
        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            ListViewHitTestInfo hitTestInfo = listView1.HitTest(e.X, e.Y);

            if ("" != hitTestInfo.Item.Text)
            {
                if (true == _AppManager.RunningMonitorProcess)
                {
                    tempText = String.Format("Already Monitor Process [{0}], Change to this Process [{1}] ?",
                                             _AppManager.TargetProcessInfo.Name, hitTestInfo.Item.Text);

                    DialogResult DlgResult = MessageBox.Show(tempText, "Question ?", MessageBoxButtons.YesNo);

                    if (DialogResult.Yes == DlgResult)
                    {
                        PrintAndWriteFileWithTime(tempText);

                        tempText = String.Format("Start Process Monitor [{0}]!!!!!!", hitTestInfo.Item.Text);
                        PrintAndWriteFileWithTime(tempText);

                        // save process info to Settings.
                        _AppManager.SaveMonitorProcessInfo();

                        SetTargetMonitorProcessInfo(hitTestInfo.Item.SubItems);
                        _AppManager.PrintTargetMonitorProcessInfo();

                        PrintAndWriteFileWithTime("Try to Start MonitorProcessWorker");
                        if (true == _AppManager._runningMonitorThread)
                        {
                            PrintAndWriteFileWithTime("Already Running MonitorProcessWorker Thread!");
                        }
                        else
                        {
                            _AppManager.StartMonitorProcessWorkerThread();
                            PrintAndWriteFileWithTime("Success to Start MonitorProcessWorker");
                        }

                        tempText = String.Format("{0}|{1}", hitTestInfo.Item.SubItems[0].Text,
                                                 hitTestInfo.Item.SubItems[3].Text);

                        MinimizeWindow();
                    }
                    else if (DialogResult.No == DlgResult)
                    {
                    }
                }
                else
                {
                    tempText = String.Format("Start Monitor [{0}] ?", hitTestInfo.Item.Text);
                    DialogResult DlgResult = MessageBox.Show(tempText, "Question ?", MessageBoxButtons.YesNo);
                    if (DialogResult.Yes == DlgResult)
                    {
                        tempText = String.Format("Start Process Monitor [{0}]", hitTestInfo.Item.Text);
                        PrintAndWriteFileWithTime(tempText);

                        string [] curMonitorProcessInfo = { hitTestInfo.Item.SubItems[2].Text,
                                                            hitTestInfo.Item.SubItems[0].Text,
                                                            hitTestInfo.Item.SubItems[3].Text };

                        _AppManager.SetTargetMonitorProcessInfo(ref curMonitorProcessInfo);

                        PrintAndWriteFileWithTime("Try to Start MonitorProcessWorker");
                        _AppManager.StartMonitorProcessWorkerThread();
                        PrintAndWriteFileWithTime("Success to Start MonitorProcessWorker");

                        MinimizeWindow();
                    }
                    else if (DialogResult.No == DlgResult)
                    {
                    }
                }
            }
        }