Solved

C# Multithreading an Application

Posted on 2009-04-13
6
507 Views
Last Modified: 2012-05-06
Hi Experts,

I have a library called AccountingPackage.dll. This library exposes a class called Statistics that has a method:
public string GetStatistics().

The method takes a considerable amount of time to execute. I have a front end application that consists of a single button which once clicked calls this method. The issue is that the app freezes while GetStatisitcs method is doing it's work. How can I multithread this application so that I can prevent the application from freezing?

Note that the Statistics class exposes an event called OnCalculatingAStatistic which is triggered multiple times during the GetStatistics operation and provides feedback information. I would also like to take this information and display it in a listbox/textbox as the carries out it's work in an async manner.

Thanks,
j
0
Comment
Question by:XPUSR
6 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 24129139

private void UrButtonClick(object sender, EventArgs e)
    {
        System.Threading.Thread t = new
        System.Threading.Thread
        (
        new ThreadStart
        (delegate
        {
            //ur call goes here
        }
        ));
        t.Start();
    }

Open in new window

0
 
LVL 15

Expert Comment

by:oobayly
ID: 24129195
In addition, any of the events called by the Statistics class probably won't be allowed to update the GUI, as they're from different thread than the one that created the GUI.
So you should use Invoke, (or BeginInvoke so the method is executed asynchronously) to update any UI controls.
private void stats_OnCalculatingAStatistic(object sender, EventArgs e){
  /* Updating label like this will cause an InvalidOperationException:
   * ie: Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.
   */
  // label1.Text = "Calculating...";
 
  // This won't cause an exception
  label1.BeginInvoke(new MethodInvoker(delegate {
    label1.Text = "Calculating...";
  }));
 
 
  
}

Open in new window

0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24130261
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:XPUSR
ID: 24133035
oobayly, label1 is in a different assembly (i.e. AccountingPractices.dll) and I cannot modify this assembly. All modifications have to be completed in the Application code of the solution.

ragi0017, can you tell me why the background worker is best? And can I use it directly from the Front End (the application) without having to modify the referenced assembly?
0
 
LVL 15

Accepted Solution

by:
oobayly earned 250 total points
ID: 24133188
I don't quite follow. From the UI, you start GetStatistics() which is in the AccountingPractices assembly. Whilst GetStatistics is running, the OnCalculatingAStatistic event is raised periodically. In this event you wish to update the UI. How would label1 be in a different assembly, it's a control I've used as an example that exists in the application UI to provide feedback to the user.

The BackgroundWorker is better because you are able to manipulate the UI directly from it's events without resorting to the BeginInoke code I provided. You'd have the following pseudo code. If you were to show us how the code is called and the OnCalculatingAStatistic handled, with the desired UI updates, we might have a better changce explaining.

HAve a look at the MSDN reference for BackgroundWorker
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
' Inside BackgroundWorker DoWork event
e.Result = AccountingPractices.GetStatistics();
 
' Inside AccountingPractices OnCalculatingAStatistic event
backgroundWorker.ReportProgress()
 
' Inside BackgroundWorker ProgressChanged event
Update UI

Open in new window

0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24133317
i am assuming that you are using a windows application and will like to update the UI control from the thread
in a windoes application this functionality cannot be achieved easily as the UI controls can be updated from the main thread only
read the following article to clear the understanding about therading in windows forms application
http://www.yoda.arachsys.com/csharp/threads/winforms.shtml
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

679 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