Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application freezes outside of Visual Studio. While starting it from Visual Studio it works

slowly I'm overworked...

I have a huge application with threading, timers, invoke (not BeginInvoke, so it is synchronous) and Application.DoEvents.

It is too much to post here and I don't know where the problem really is.

Every Method of mine is inside a try catch. Every catch is logged.

If I start my application from Visual Studio (F5) or while profiling it via Ants there is no Problem. The Application runs since some days. But as soon as I start the same debug version via windows explorer it freezes every few hours. It freezes without any exception or so. If I attach visual studio to this application and break it, it stops on Application.Run(new Form1());

I'm really confused and have no idea to repair it.

It is a .net 3.5 winforms application

It looks like one thread hangs here:

if (grabber.InvokeRequired)
{
    Console.WriteLine("grabber.InvokeRequired");
    this.Invoke((MethodInvoker) delegate { grabber.Navigate("http://www.google.de"); }); // <-- hang
}
else
{
    grabber.Navigate(ig.StartUrl);
}

this snippet is part of an timer event

_timeout = new System.Timers.Timer(10000);
_timeout.Elapsed += new ElapsedEventHandler(OnWatchDogBark);

Edit

A sample for DoEvents(). This is in a lock() and in an invoke

grabber.DocumentCompleted -= grabber_DocumentCompleted;
grabber.Navigate("http://www.google.de");

while (grabber.ReadyState != WebBrowserReadyState.Complete)
{
    timeout--;
    Application.DoEvents();
    Thread.Sleep(200);

    if (timeout < 0)
    {
        timeout = 50;
        grabber.Navigate("http://www.google.de");
    }
}

Currently I use the System.Windows.Forms.Timer and some locks but there is no improvement.

Okay I used WinDbg to get some informations

Edit: 14.06.2012

!threads

                                      PreEmptive   GC Alloc           Lock
       ID OSID ThreadOBJ    State     GC       Context       Domain   Count APT Exception
   0    1 37ec 007cab18      6020 Enabled  00000000:00000000 007c8510     0 STA System.ArgumentException (02762ba8)
   2    2 85b8 007d7c38      b220 Enabled  00000000:00000000 007c8510     0 MTA (Finalizer)
XXXX    3    0 06e9f548      9820 Enabled  00000000:00000000 007c8510     0 Ukn
  21    5 3464 0d6dc598   200b020 Enabled  28cb5820:28cb5fe8 007c8510     0 MTA
  22    6 62b0 0d6db9e0   200b220 Enabled  00000000:00000000 007c8510     0 MTA
  23    7 8e58 0d6db5f8    80a220 Enabled  00000000:00000000 007c8510     0 MTA (Threadpool Completion Port)
XXXX    4    0 06f62d40   1801820 Enabled  00000000:00000000 007c8510     0 Ukn (Threadpool Worker)
XXXX    f    0 132a3290   1801820 Enabled  00000000:00000000 007c8510     0 Ukn (Threadpool Worker)
XXXX   10    0 132a3678   1801820 Enabled  00000000:00000000 007c8510     0 Ukn (Threadpool Worker)
XXXX    e    0 132a26d8   1801820 Enabled  00000000:00000000 007c8510     0 Ukn (Threadpool Worker)
XXXX    9    0 0d6db210   1801820 Enabled  00000000:00000000 007c8510     0 Ukn (Threadpool Worker)

!dlk

Examining SyncBlocks...
Scanning for ReaderWriterLock instances...
Scanning for holders of ReaderWriterLock locks...
Scanning for ReaderWriterLockSlim instances...
Scanning for holders of ReaderWriterLockSlim locks...
Examining CriticalSections...
Could not find symbol ntdll!RtlCriticalSectionList.
No deadlocks detected.
like image 536
masterchris_99 Avatar asked Jun 05 '12 09:06

masterchris_99


2 Answers

Could be a possible Deadlock in a background thread. Try looking at other threads that could block your app.

Toolbar -> Debug -> Windows -> Threads

http://msdn.microsoft.com/en-us/library/w15yf86f.aspx

There should be multiple threads and if you double click one you see the line where it is stopping your app.

And if you this line in your code:

Control.CheckForIllegalCrossThreadCalls = false;

Set it to true again. A possible cause for dead locks are background thread accessing controls.

Instead of writing this from a backgroud threads.

button1.Text = "hello"

write this.

this.Invoke(() => button1.Text = "hello");
like image 184
Jürgen Steinblock Avatar answered Sep 28 '22 09:09

Jürgen Steinblock


If it's freezing, you are likely looking at a deadlock. One of the best ways I have found to find a deadlock is to use a crash dump and sosex.

Here's a good article on using this technique (it's asp.net, but the same principles apply): http://blogs.msdn.com/b/tess/archive/2010/04/27/debugging-a-classic-readerwriterlock-deadlock-with-sosex-dll.aspx

let the app run until it freezes, and take a hang dump: http://blogs.msdn.com/b/tess/archive/2006/10/16/net-hang-debugging-walkthrough.aspx

like image 39
JMarsch Avatar answered Sep 28 '22 09:09

JMarsch