User Interface still locks up when I use threading


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
Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
I have to agree with CodeCruiser:  using a delegate or Invoke do not compromise, themselves, threading. You should actually be spawning a Thread, using the Task library, use a BeginXXX and corresponding EndXXX method, or use a BackgroundWorker. None of these are visible in the code you posted, so where is the threading?
CodeCruiserConnect With a Mentor Commented:
I may be missing something but where do you declare and use a thread?

You may want to look into BackGroundworker control
PingPhotonicsAuthor Commented:
Sorry CodeCruiser the okB_click function should be;

private void okB_Click(object sender, EventArgs e)
   doCalculation(rect, firstBand, secondBand, dataType, theProgressBar);
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

PingPhotonicsAuthor Commented:

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.
käµfm³d 👽Commented:
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.
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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.
PingPhotonicsConnect With a Mentor Author Commented:
Ok Ive just realised I was to call my delegate using BeginInvoke. It does what I want now.


I also made a bit of a mess of my code snippet.
PingPhotonicsAuthor Commented:
I forgot to use "BeginInvoke" to invoke my delegate. This was a key mistake.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.