User Interface still locks up when I use threading

Posted on 2012-08-16
Last Modified: 2012-08-21

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
Question by:PingPhotonics
    LVL 83

    Assisted Solution

    I may be missing something but where do you declare and use a thread?

    You may want to look into BackGroundworker control

    Author Comment

    Sorry CodeCruiser the okB_click function should be;

    private void okB_Click(object sender, EventArgs e)
       doCalculation(rect, firstBand, secondBand, dataType, theProgressBar);
    LVL 74

    Accepted Solution

    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?

    Author Comment


    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.
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    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.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    "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.

    Assisted Solution

    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.

    Author Closing Comment

    I forgot to use "BeginInvoke" to invoke my delegate. This was a key mistake.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now