views:

71

answers:

5

I have a (rather large) application which occasionally gets 'stuck' whilst loading - with controls half painted etc.

From what I have managed to work out from logging, this happens after the end of the _Load event.

The only time it happened to me when running from Visual Studio, I tried doing a pause, and it just took me to the Application.Run line in main, so that didn't help much.

TaskMan doesn't say that the application is 'not responding' - but maybe I'm not waiting long enough for it to work that out (anyone know how long it takes?)

I'm going to carry on trying to narrow things down (ProcessExplorer on the test machine etc), but if anyone has any ideas for directions in which to search, before I start putting printfs all through my code...

+1  A: 

Use a profiler like dotTRACE or ANTS. Also when you pause, if you have the code running on another thread you can switch to the other thread by choosing:

Debug --> Windows --> Threads.

RichardOD
A: 

You can try to attach a profiler and see where time is spent during startup. That may point you to a good candidate.

Oded
A: 

By the sounds of it you could have an infinite loop somewhere. I would look at any long winded processes and or loops that happen after your Load event.

James
Yeah, but an infinite loop only sometimes, that's what I can't understand.
Benjol
Yeah so I would review all your loops i.e. step through them and double check that they don't rely on any particular flags being set. Again its just a shot in the dark as there is no code for me to debug :)
James
A: 

If the problem doesn't occur within the debugger, you can run the program, wait for it to hang and then from within Visual Studio go to Attach to Process... (under the Tools menu in VS2005).

Then look through the threads as the other poster mentioned.

Mongus Pong
+1  A: 

The diagnostics you provided matches the behavior. The form's Load event would be the last bit of your code running before the form paints itself. The Shown event would be next. The debugger would indeed stop on the Application.Run() call, that's the last bit of code that you wrote for which the debugger has source code info.

The key is to carefully examine the Call Stack window, there ought to be additional stack frames above the Application.Run one. The top one is the trouble maker if the code that causes the hang is managed code. You won't have source code for it, getting setup with the Microsoft Reference Source server is important if you need that code.

But it isn't very likely to be managed code, the vast majority of Paint event handlers are unmanaged code, either in Windows or some kind of ActiveX control. To get insight into that kind of code you'll need to enable unmanaged debugging. Project + Properties, Debug tab. Also, Tools + Options, Debugger, turn off "Just my code". Getting setup to debugging symbols from Microsoft's symbol server can be important to make sense of the stack trace, do so if you only get hex addresses in the stack trace.

Next, you ought to be able to see what particular control is causing this problem by observing what is getting painted and what is not. Controls get painted in Z-order, the one you don't see, or only see partially, ought to be the trouble maker. Painting hangs in Windows or .NET controls are unheard of, suspect any kind of ActiveX control on your form.

Hans Passant