Click to See Complete Forum and Search --> : UI stops reponding after a while, threading issue


atuvy
September 9th, 2009, 11:42 AM
I have an application in which all of the functionality is done in a dll. The dll includes two threads which perform some tasks continuously and it works well. UI is nothing more than a form with a single button which initializes the dll and starts the threads.
After running for a few minutes the UI freezes and cannot be moved or closed but the threads continue to run and do their job. I know that it is a long shot without seeing the code but can anybody offer an explanation for that.

dannystommen
September 9th, 2009, 12:08 PM
Start the long running task in a backgound thread. UI will not freeze now.

But remember to stop the thread when you close your form

eclipsed4utoo
September 9th, 2009, 12:51 PM
the BackgroundWorker (http://msdn.microsoft.com/en-us/library/cc221403(VS.95).aspx) class is a good place to start.

Arjay
September 9th, 2009, 01:03 PM
I have an application in which all of the functionality is done in a dll. The dll includes two threads which perform some tasks continuously and it works well. UI is nothing more than a form with a single button which initializes the dll and starts the threads.
After running for a few minutes the UI freezes and cannot be moved or closed but the threads continue to run and do their job. I know that it is a long shot without seeing the code but can anybody offer an explanation for that.Does the UI threads wait for the worker threads to finish? If so, then you are preventing the UI thread from pumping messages so it appears frozen. You would have the same effect if you wrote a simple form app with a button and in the button handler you put a long Sleep statement. After a short time, the app would appear frozen.

A fix in your case is to disable the UI functionality prior to starting the dll threads and wait for a threads completed event to reenable the UI. If you can't modify the dll source code to add a completion event, then disable the controls, create a backgroundworker thread in your UI, and wait for the backgroundworker completion event to reenable the controls. Inside the backgroundworker you would call into your dll.

atuvy
September 9th, 2009, 01:30 PM
I tried to run it as a background thread and it still freezes, do I need to do anything else besides what I listed below?

_workerThread.IsBackground = true;

atuvy
September 9th, 2009, 01:33 PM
the background thread runs indefinitely and perform some tasks so I cannot really disable controls and wait for the thread to complete

eclipsed4utoo
September 9th, 2009, 01:49 PM
can we see the rest of your code that pertains to the background worker?

atuvy
September 9th, 2009, 02:01 PM
Yes, I can see the code and modify it if needed

eclipsed4utoo
September 9th, 2009, 02:10 PM
Yes, I can see the code and modify it if needed

I meant can you post it for us to see. we don't need to see the code from the dll, just the code that deals with the backgroundworker.

atuvy
September 9th, 2009, 02:19 PM
This is the code that starts the threads on my form

public void StartWorkerThread()
{

_workerThread = new Thread(objFWDownloadAPI.scanNewGroup);
_workerThread.IsBackground = true;
_workerThread.Start();


_statusThread = new Thread(objFWDownloadAPI.scanGetStatus);
_statusThread.IsBackground = true;
_statusThread.Start();

}

This is the loop that runs on one of the threads, the contents of the loop involves database access, I removed it but if it is needed I can post some scaled down version. The loop runs in a dll

public void scanGetStatus()
{


while (true)
{


Thread.Sleep(10000);
}
}

Arjay
September 9th, 2009, 03:10 PM
What does the UI thread do after calling the StartWorkerThread method?

Arjay
September 9th, 2009, 03:20 PM
I tried to run it as a background thread and it still freezes, do I need to do anything else besides what I listed below?

_workerThread.IsBackground = true;If you are starting worker threads, there are really only two reasons for the UI thread to freeze.

1) You are waiting for the threads to finish in the UI (effectively putting the UI thread to sleep).
2) The worker threads are starving out the UI thread. They could do this by spinning in a tight loop, or by having a higher priority than the UI thread.

atuvy
September 9th, 2009, 03:36 PM
After starting the threads the UI does nothing.
Both threads have a 10 seconds sleep in the loop. What is interesting is that while the threads do actual work the UI does not freeze but sometime after the thread are done with their tasks and just loop with sleep the UI freezes.

Arjay
September 9th, 2009, 04:04 PM
After starting the threads the UI does nothing.
Both threads have a 10 seconds sleep in the loop. What is interesting is that while the threads do actual work the UI does not freeze but sometime after the thread are done with their tasks and just loop with sleep the UI freezes.Do the threads attempt to update the UI after completing?

If not and there is no interaction between the UI thread and the worker threads, then look for a problem elsewhere in the UI thread (because the problem doesn't seem to be related to threading).

atuvy
September 9th, 2009, 04:09 PM
The threads do not interact with the UI so it is somewhat of a mystery to me.

Arjay
September 9th, 2009, 04:24 PM
The threads do not interact with the UI so it is somewhat of a mystery to me.If the threads don't interact, does the UI still freeze if you comment out the threading code?

atuvy
September 9th, 2009, 04:28 PM
I tested it a few weeks ago and it did not freeze, I will try it again shortly