Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Multithreading an Application

Posted on 2009-04-13
6
Medium Priority
?
511 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 1000 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

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

661 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