Application.Run() leads to application hanging

戏子无情 提交于 2019-12-13 21:24:41

问题


When I start application without specific form e.g.

Application.Run();

And then I create form, after user closes it, the process just hangs.

This happens outside of Visual Studio.

I tried to put Application.Exit() and/or Application.ExitThread() in form's Form_Closing event, but it still hangs.

Edit: Using custom ApplicationContext unfortunately doesn't work.

FYI, I'm not using any threads nor BackgroundWorkers.


回答1:


Use Tools + Attach to Process to attach the debugger to the hung process. Debug + Break All. Debug + Windows + Threads, double-click the Main thread and look at its call stack to see what it is doing. Post the stack trace in your question if that doesn't help.




回答2:


From the MSDN for the parameterless Application.Run():

Most Windows Forms developers will not need to use this version of the method. You should use the Run(Form) overload to start an application with a main form, so that the application terminates when the main form is closed. For all other situations, use the Run(ApplicationContext) overload, which supports supplying an ApplicationContext object for better control over the lifetime of the application.

So, short answer, try specifying an ApplicationContext object, or just Run() the form you are creating and showing. Maybe a more concrete example of why you're trying to open the message loop without tying it to a form would help. Are you opening several forms? Is this an introductory form like a splash screen or a login?




回答3:


Try using the overload of Application.Run(new MyForm()) instead, though Application.Exit() should be working for you as described here.



来源:https://stackoverflow.com/questions/4503812/application-run-leads-to-application-hanging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!