Solved

C# Multithreading an Application

Posted on 2009-04-13
6
502 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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 …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

13 Experts available now in Live!

Get 1:1 Help Now