1

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.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
mnn
  • 1,952
  • 4
  • 28
  • 51
  • @BestArmy: I think I did (according to my comment below), but I don't remember how. It's been more than 4 years ago. In past year I've worked only on one small WinForms application, otherwise it's been only WPF/XAML for me. – mnn May 08 '15 at 16:36
  • Tod bad you didn't post the answer here. You sure you cant remember? – BestR May 08 '15 at 16:38
  • Yeah, that's too bad, I didn't. I am sorry, but I really don't remember, what I did back then. I don't even remember, what application it was, otherwise I could try looking up its source code. Unfortunately, you have to figure it out on your own (I believe you've tried answers here). – mnn May 08 '15 at 16:43

3 Answers3

2

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • It's hung at Application.Run() line. – mnn Dec 21 '10 at 21:09
  • :) Really, there's just External code and the line in Program.cs (Application.Run()). Trust me, I've done some work with Call stack. – mnn Dec 21 '10 at 21:15
  • Okay, then you'll be comfortable with turning unmanaged debugging on. Setup the Microsoft Symbol Server. Project + Properties, Debug, tick "Unmanaged". The call stack will now show you what unmanaged code is running. – Hans Passant Dec 21 '10 at 21:17
  • 5
    Care to share what you did for the benefit of others? – Chris Dunaway Dec 22 '10 at 18:42
  • @mnn Care to share what you did to figure it out? (not sure if the other comment notified you) – sirdank Jan 18 '16 at 20:52
  • @sirdank I'm truly sorry, but I have no idea, what the root issue was. The first comment did notify me, I just didn't have time to respond at the time (I didn't remember then remember either). I did respond to a comment above: https://stackoverflow.com/questions/4503812/application-run-leads-to-application-hanging/4503874?noredirect=1#comment48367651_4503812. – mnn Feb 18 '16 at 18:28
1

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?

KeithS
  • 70,210
  • 21
  • 112
  • 164
0

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

Steve Danner
  • 21,818
  • 7
  • 41
  • 51