Don't Break The Build Software development articles and tutorials

8Apr/116

Excel And C# – Interop With .Net 4 Part 2 – The Background Worker

This is a continuation from my first post on Excel and C#, so if you haven't read that yet, you should definitely check it out.

This part of tutorial is going to push Excel to the side for a bit (don't worry, we'll get back to it next time) and we will focus on the Background Worker. When reading from large Excel files synchronously the User Interface can become unresponsive, which is a big no-no, because the user may not know what's happening. Is the program crashing? Is it working hard and just ignoring me? Enter the background worker.

The Background Worker class is very easy to use and provides many events that allow us to hook it up to the User Interface (such as progress bars). Let's start by declaring a simple class and declaring a background worker:

 <span style="color:#0000ff">using</span><span style="color:#000000">  System;

 <span style="color:#0000ff">using</span><span style="color:#000000">  System.Collections.Generic;

 <span style="color:#0000ff">using</span><span style="color:#000000">  System.Linq;

 <span style="color:#0000ff">using</span><span style="color:#000000">  System.Text;

 <span style="color:#0000ff">using</span><span style="color:#000000">  System.ComponentModel;

 

 <span style="color:#0000ff">namespace</span><span style="color:#000000">  test

 <span style="color:#000000">{</span>

     <span style="color:#0000ff">public</span><span style="color:#000000">  <span style="color:#0000ff">class</span><span style="color:#000000">  <span style="color:#2b91af">test</span><span style="color:#000000"> 

     <span style="color:#000000">{</span>

         <span style="color:#0000ff">public</span><span style="color:#000000">  test()

         <span style="color:#000000">{</span>

             <span style="color:#2b91af">BackgroundWorker</span><span style="color:#000000">  bw = <span style="color:#0000ff">new</span><span style="color:#000000">  <span style="color:#2b91af">BackgroundWorker</span><span style="color:#000000"> ();

         <span style="color:#000000">}</span>

     <span style="color:#000000">}</span>

 <span style="color:#000000">}</span>

 

 

The only required namespace is System.ComponentModel. Once we have that, we would be good to go. Next we have to actually wire-up the events DoWork and RunWorkerCompleted. Below is our new Constructor class for our Test class:

         <span style="color:#0000ff">public</span><span style="color:#000000">  test()

         <span style="color:#000000">{</span>

             

             <span style="color:#2b91af">BackgroundWorker</span><span style="color:#000000">  bw = <span style="color:#0000ff">new</span><span style="color:#000000">  <span style="color:#2b91af">BackgroundWorker</span><span style="color:#000000"> ();

             bw.WorkerReportsProgress = <span style="color:#0000ff">true</span><span style="color:#000000"> ;    <span style="color:#008000">// Allows the background worker to report progress</span><span style="color:#000000"> 

             bw.DoWork += <span style="color:#0000ff">new</span><span style="color:#000000">  <span style="color:#2b91af">DoWorkEventHandler</span><span style="color:#000000"> (bw_DoWork); <span style="color:#008000">// Wire up the DoWork event</span><span style="color:#000000"> 

             bw.RunWorkerCompleted += <span style="color:#0000ff">new</span><span style="color:#000000">  <span style="color:#2b91af">RunWorkerCompletedEventHandler</span><span style="color:#000000"> (bw_RunWorkerCompleted); <span style="color:#008000">// Wire up the RunWorkerCompleted</span><span style="color:#000000"> 

         <span style="color:#000000">}</span>

 

Now for the meat and potatoes of this background worker. Since this is just a test, we will do trivial things (we will use it to work with Excel next time). Here is our DoWork:

         <span style="color:#0000ff">void</span><span style="color:#000000">  bw_DoWork(<span style="color:#0000ff">object</span><span style="color:#000000">  sender, <span style="color:#2b91af">DoWorkEventArgs</span><span style="color:#000000">  e)

         <span style="color:#000000">{</span>

             <span style="color:#008000">// Long for loop to simulate &quot;heavy lifting&quot;</span><span style="color:#000000"> 

             <span style="color:#0000ff">int</span><span style="color:#000000">  j = 0;

             <span style="color:#0000ff">for</span><span style="color:#000000">  (<span style="color:#0000ff">int</span><span style="color:#000000">  i = 0; i &lt; 10000; i++)

             <span style="color:#000000">{</span>

                 j = 0;

             <span style="color:#000000">}</span>

 

             e.Result = j;

         <span style="color:#000000">}</span>

 

Notice the e.Result. We will set the result of the DoWork to that so we can get the results in our RunWorkerCompleted (as seen below):

         <span style="color:#0000ff">void</span><span style="color:#000000">  bw_RunWorkerCompleted(<span style="color:#0000ff">object</span><span style="color:#000000">  sender, <span style="color:#2b91af">RunWorkerCompletedEventArgs</span><span style="color:#000000">  e)

         <span style="color:#000000">{</span>

             <span style="color:#008000">// Do something with the results</span><span style="color:#000000"> 

             <span style="color:#2b91af">MessageBox</span><span style="color:#000000"> .Show(<span style="color:#a31515">&quot;The results are: &quot;</span><span style="color:#000000">  + e.Result);

         <span style="color:#000000">}</span>

 

 

Notice how we use e.Result in the message box. We are all done and ready to go with the exception of one thing: how do we actually get this to run? Fear not, it is quite easy! All you have to do is as follows:

 bw.RunWorkerAsync();    <span style="color:#008000">// Run the background worker!

 

Now when we run that, we should get the following result:



Next time we will combine the Background Worker and the Excel tutorial to load an Excel file in the background.

Tagged as: Leave a comment
Comments (6) Trackbacks (0)
  1. Enjoyed examining this, very good stuff, thankyou .

  2. Interesting article at least I think so. Thnx for providing that info.

    Tom Davidson
    2.4 ghz jammer

  3. Greetings, Thank you for composing this excellent article content. I am quite pleased with your writing style and the way you get across your thoughts. You’ve got a excellent future in producing blogs, that is certain. Continue the extraordinary work.

  4. Great article if you ask me. Thanks for sharing this info.

    Suissy Phillsen

  5. Hi,
    The code sample has HTML tags in it making it hard to read

  6. Same complain, the page is shown withe the HTML tags, that is weird





Leave a comment

No trackbacks yet.