守护进程函数——内部的小范围try catch 增强了 while死循环执行的 可靠性

void Watch()
        {
            try
            {
                LogHelper.WriteLog("WatchService Watch Start!");
                while (true)
                {
                    try
                    {
                        if (ReadConfig() != "true")
                        {
                            HideTaskBarAndStartButton(0);
                        }
                        else
                        {
                            HideTaskBarAndStartButton(1);
                        }
                    }
                    catch (Exception)
                    {

                              //这个try catch 用得非常好 ,不能因小(显示/隐藏任务栏)失大(while死循环崩溃)
                    }

                    Process[] process = Process.GetProcessesByName("Zxt.TaxSelfHelp.Client");
                    //LogHelper.WriteLog("WatchService Watch Client process.Length:" + process.Length);
                    if (process.Length == 0)
                    {
                        process = Process.GetProcessesByName("Zxt.TaxSelfHelp.Update");
                        //LogHelper.WriteLog("WatchService Watch Update process.Length:" + process.Length);
                        if (process.Length == 0)
                        {
                            LogHelper.WriteLog("Process.Start " + Application.StartupPath + "\Zxt.TaxSelfHelp.Client.exe");
                            //ProcessStartInfo processStartInfo = new ProcessStartInfo();
                            //processStartInfo.Verb = "runas";
                            //processStartInfo.FileName = Application.StartupPath + "\Zxt.TaxSelfHelp.Client.exe";
                            //Process.Start(processStartInfo);

                            Process proc = new Process();
                            proc.StartInfo.FileName = "Zxt.TaxSelfHelp.Client.exe";
                            proc.StartInfo.WorkingDirectory = Application.StartupPath;
                            proc.Start();
                        }
                    }
                    Thread.Sleep(Second * 1000);
                }

            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("WatchService", ex);
            }
        }

原文地址:https://www.cnblogs.com/changbaishan/p/10266549.html