守护进程函数——内部的小范围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);
            }
        }

posted @   长白山  阅读(260)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
历史上的今天:
2015-01-14 光标或焦点相关的属性(不断更新中。。。。。。)
2014-01-14 DataTemplate——数据模板的一个典型例子
2014-01-14 TileMode(平铺模式) 枚举的成员:
2014-01-14 Resource——资源的总结
2014-01-14 WPF:自定义ListBox的选择样式
点击右上角即可分享
微信分享提示