Solved

C# Multithreading an Application

Posted on 2009-04-13
6
508 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
[X]
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
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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