VB.NET中Application.Restart()的替代方法是什么? [英] What is the Alternative to Application.Restart() in VB.NET ?
本文介绍了VB.NET中Application.Restart()的替代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我使用Application.Restart然后它关闭应用程序并且它不会立即启动新实例。为什么会这样?我正在使用visual studio 2012和.net 2.0。请给我一个解决方案。有没有其他方法来重新启动vb.net 2.0应用程序。
if i use Application.Restart then it's Shuts down the application and it does not start a new instance immediately. why it is happening ? i'm using visual studio 2012 and .net 2.0. Please give me a solution. is there any other way to restart vb.net 2.0 application.
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
Application.Restart()
End Sub
以下是ApplicationEvents.vb文件内容
Below is the ApplicationEvents.vb file content
Namespace My
' The following events are available for MyApplication:
'
' Startup: Raised when the application starts, before the startup form is created.
' Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
' UnhandledException: Raised if the application encounters an unhandled exception.
' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
Partial Friend Class MyApplication
End Class
End Namespace
推荐答案
重启失败的原因:
你在表单关闭事件上有一个事件可以阻止关闭/重启。
你有一个只允许单个应用程序实例的检查。
解决方案:在调用restart之前删除处理程序并删除单实例检查。
祝您好运!
Reasons for restart to fail:
You have an event on form close event that prevents shutdown/restart.
You have a check that allows only a single instance of the application.
Solution: Remove the handlers and remove the single instance check before calling restart.
Good luck!
原因很可能是您在应用程序启动代码中进行了单实例检查。显然应用程序必须在它仍在运行时重新启动,因此旧应用程序仍在运行且新实例正在启动的时间很短,但由于单实例检查而立即退出。我建议的解决方案:如果您的应用程序检测到已经运行的自身实例,请让它休眠1-2秒(或更长时间,如果您的应用程序需要时间关闭)并在延迟后再次检查 - 然后只有在其他实例仍在运行。
The reason is most probably that you have a single-instance-check in your application startup-code. Obviously the application has to restart itself while it is still running, so there's a small timeframe in which the old application is still running and the new instance is starting up, but immediately quitting because of that single-instance-check. My proposed solution: In case your application detects an already running instance of itself, let it sleep for 1-2 seconds (or longer, if your application needs time to shut down) and check again after that delay - and then only exit if the other instance is still running.
是的先生,我在表单关闭事件上有一个事件。我删除了那段代码。现在它工作正常。谢谢大家......非常感谢你。
Yes sir, i had an event on form close event. I removed that code. Now it's working fine. Thank u all... Thank u so much.
这篇关于VB.NET中Application.Restart()的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文