Link to home
Start Free TrialLog in
Avatar of PingPhotonics
PingPhotonicsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

User Interface still locks up when I use threading

Hello

I'm developing an MDI application in C#.NET using Visual Studio 2008

When the user clicks a button on a child window a time-consuming calculation commences. It takes about 20 seconds. Obviously I don't want the user interface locked up for this length of time.

So what Ive done is use a delegate. This performs the calculation and also updates a progress bar on the child window. I've used another delegate within this delegate to update the progress bar

The progress bar updates nice and smoothly as expected. However if I try and click anywhere else on my application the UI locks up and the cursor goes "busy". So something is obviously not working. The time consuming calculation contains several while loops. Ive noticed that if I add "application.doEvents()" in the loops my UI becomes responsive, if a bit sticky. However I don't like this solution. I don't think I should have to add application.doEvents().

 I also do writing to a file so not sure if that is the problem

The 2 delegates I use are;
 
public delegate bool doCalculation(Rectangle selectionRect, int firstBand, int secondBand, string dataType, ProgressBarX theBar);

public delegate bool updateProgressBarDelegate(ProgressBarX theBar, int theValue);


Ive attached a code snippet below. The function in OkB_Click() starts off my calculation.
 Ive taken out some error checking and simplified it slightly but the structure remains. Im thinking I need another delegate for the code within the while loops possibly?

Can anyone spot why this isn't doing what I want? Thanks.code.txt
SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PingPhotonics

ASKER

Sorry CodeCruiser the okB_click function should be;


private void okB_Click(object sender, EventArgs e)
{
   doCalculation(rect, firstBand, secondBand, dataType, theProgressBar);
}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi

OK maybe Ive implemented this wrongly. Ive assumed that when the user clicks the button then doCalculation() is spawned on another thread (its defined as a delegate) and the UI thread is free and reponsive to user input.

I'll try implementing this as a Background worker and report back. Thanks.
A delegate is an object which wraps a function, and yes event handlers are treated as delegates when you add them to an object's event list. But there is no implicit threading involved with delegates. You do use delegates in conjunction with threading, but they do not encompass threading.
"Ive assumed that when the user clicks the button then doCalculation() is spawned on another thread (its defined as a delegate)"

A delegate is simply a "pointer to a method".  The button click runs in the main UI thread, so calling the delegate from there also causes that delegate to run in the main UI thread as well.  This is the same as if you had called the method pointed to by the delegate directly.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I forgot to use "BeginInvoke" to invoke my delegate. This was a key mistake.