When I start application without specific form e.g.
And then I create form, after user closes it, the process just hangs.
This happens outside of Visual Studio.
I tried to put
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.Answer1:
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.Answer2:
From the MSDN for the parameterless Application.Run():<blockquote>
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.</blockquote>
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?Answer3:
Try using the overload of
Application.Run(new MyForm()) instead, though Application.Exit() should be working for you as described <a href="http://msdn.microsoft.com/en-us/library/ms157900.aspx" rel="nofollow">here</a>.