Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# Multithreading an Application

Posted on 2009-04-13
6
506 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

765 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