winfrom中Application.Restart()

System.Windows.Forms.Application.Restart();方法系统实现自动重启。

            if (FrmDialog.ShowDialog(this, "确定退出登录吗?", "提示", false, false, false, null, DialogType.YesNo) == DialogResult.OK)
            {
                System.Windows.Forms.Application.Restart();
            }

 

花了接近一天的时间,才研究出来这个系统自动重启时好时坏的问题。  

应用.net自带的Application.Restart();有时候重新启动好用,而有时就是起不来,特别启动次数多了和电脑比较老旧时老出现。查找了好多相关的资料,都没能尝试成功。

但是不管怎样我觉得这个说的还算是对我有用粘贴过来。

  If Application.Restart is called from any thread in a normally run Windows Forms application, the application shuts down and restarts. 
In a ClickOnce deployed application, this can be used to cause the application to restart after an update has been downloaded using the ApplicationDeployment class. 
However, if you call Application.Restart from a non-UI thread (i.e a thread pool thread in the BackgroundWorker.DoWork event), the application is shut down but not restarted. 

 

感觉网上很不赞成应用Application.Restart();方法的,因为,可能导致它不好用的原因很多,会很莫名其妙,但是系统确实就有这样的需求,因此,只能不得已而为之。

这次解决的办法,我总结主要是这个原因,在系统要求自动重启的时候,首先要关闭原有的所有进程,但是重启的这个方法,会因为在没有完全关闭的时候就启动,这样方法本身冲突发生异常就会被关闭,因此,重新启动失败。最后,将Program中的启动线程挂起1秒,问题便解决了。

            //解决Restart方法冲突问题
            System.Threading.Thread.Sleep(1000);

 

posted @ 2021-01-21 14:14  吴枫  阅读(1278)  评论(0编辑  收藏  举报