Sie sind auf Seite 1von 12

2/21/2014

C# BackgroundWorker Tutorial

C# BackgroundWorker
BackgroundWorker makes threads easy to implement in Windows Forms. Intensive tasks need to be done on another thread so the UI does not freeze. It is necessary to post messages and update the user interface when the task is done. Thread

Go

C#: Windows

Steps
In the image, you see the Visual Studio designer view. The fly-out panel is the Toolboxit contains links for all kinds of controls. It also contains the BackgroundWorker link, which we can use in the following steps. First, click on BackgroundWorker. You will need to double-click on BackgroundWorker link in the Toolbox. Look at the gray bar near the bottom of your window. A BackgroundWorker will appear there.
http://www.dotnetperls.com/backgroundworker 1/12

2/21/2014

C# BackgroundWorker Tutorial

Second: Windows Forms Highlight backgroundWorker1. Click on the backgroundWorker1 item in the gray bar on the bottom. Now, C# Visual Studio VB Net look at the Properties panel. Third: Look for the lightning bolt. You will see a lightning bolt icon in the Properties pane. This is Microsoft's icon for events. Tip: BackgroundWorker is event-driven, so this is where we will make the necessary events to use it. Then, double-click on DoWork. Sorry, but we have to begin working. Double-click on DoWork, which will tell Visual Studio to make a new "work" method. In this method, you will put the important, processor-intensive stuff.

http://www.dotnetperls.com/backgroundworker

2/12

2/21/2014

C# BackgroundWorker Tutorial

The rectangles show the tray at the bottom containing the backgroundWorker1 icon, and the Properties panel on the right. The lightning bolt helps us easily add events to the BackgroundWorker. Tip: This UI is far better than trying to type the methods in manually. It requires less effort on your part.
http://www.dotnetperls.com/backgroundworker 3/12

2/21/2014

C# BackgroundWorker Tutorial

DoWork
The DoWork method is like any other event handler. Here we must look at the C# view of your file, where we will see the DoWork method. You should see that the backgroundWorker1_DoWork event is generated when you double-click on DoWork. For testing, let's add a Thread.Sleep command there. The Thread.Sleep method will pause the execution of the BackgroundWorker, but does not consume the entire CPU. It will show us threading is functioning. Sleep
E x a m p l et h a ts h o w sD o W o r ke v e n th a n d l e r :C # u s i n gS y s t e m ; u s i n gS y s t e m . C o m p o n e n t M o d e l ; u s i n gS y s t e m . W i n d o w s . F o r m s ; u s i n gS y s t e m . T h r e a d i n g ; n a m e s p a c eW i n d o w s F o r m s A p p l i c a t i o n 1 { p u b l i cp a r t i a lc l a s sF o r m 1:F o r m { p u b l i cF o r m 1 ( ) { I n i t i a l i z e C o m p o n e n t ( ) ; } p r i v a t ev o i db a c k g r o u n d W o r k e r 1 _ D o W o r k ( o b j e c ts e n d e r ,D o W o r k E v e n t A r g se ) {
http://www.dotnetperls.com/backgroundworker 4/12

2/21/2014

C# BackgroundWorker Tutorial

} }

T h r e a d . S l e e p ( 1 0 0 0 ) ;/ /O n es e c o n d .

Properties
I want to emphasize the Argument, Result, and the RunWorkerAsync methods. These are the properties of BackgroundWorker required to accomplish anything. I show the properties as you would reference them in your code.
B a c k g r o u n d W o r k e rp r o p e r t yl i s t :C # D o W o r k E v e n t A r g se C o n t a i n se . A r g u m e n ta n de . R e s u l t . I ti su s e dt oa c c e s st h o s ep r o p e r t i e s . e . A r g u m e n t U s e dt og e tt h ep a r a m e t e rr e f e r e n c er e c e i v e db yR u n W o r k e r A s y n c . e . R e s u l t C h e c kt os e ew h a tt h eB a c k g r o u n d W o r k e rp r o c e s s i n gd i d . b a c k g r o u n d W o r k e r 1 . R u n W o r k e r A s y n c C a l l e dt os t a r tap r o c e s so nt h ew o r k e rt h r e a d .

RunWorkerAsync
http://www.dotnetperls.com/backgroundworker 5/12

2/21/2014

C# BackgroundWorker Tutorial

We can add instructions to the BackgroundWorker by adding arguments and return values. We need to add arguments, invoke the BackgroundWorker, and then receive the results of the thread. Tip: Changing variables from multiple threads at once often leads to bugs. A variable can become invalid.
E x a m p l et h a ts h o w sR u n W o r k e r A s y n cc a l l :C # p u b l i cp a r t i a lc l a s sF o r m 1:F o r m { p u b l i cF o r m 1 ( ) { I n i t i a l i z e C o m p o n e n t ( ) ; / / / /E x a m p l ea r g u m e n to b j e c t / / T e s t O b j e c tt e s t=n e wT e s t O b j e c t { O n e V a l u e=5 , T w o V a l u e=4 } ; / / / /S e n da r g u m e n tt oo u rw o r k e rt h r e a d / / b a c k g r o u n d W o r k e r 1 . R u n W o r k e r A s y n c ( t e s t ) ;

http://www.dotnetperls.com/backgroundworker

6/12

2/21/2014

C# BackgroundWorker Tutorial

/ / /< s u m m a r y > / / /T h et e s tc l a s sf o ro u re x a m p l e . / / /< / s u m m a r y > c l a s sT e s t O b j e c t { p u b l i ci n tO n e V a l u e{g e t ;s e t ;} p u b l i ci n tT w o V a l u e{g e t ;s e t ;} }

An example argument is created. TestObject in the above code is an example object. You will have something more important and complex in your program. The syntax above is just C# collection initializer syntax. Tip: RunWorkerAsync can be called anywhere in your code. We use a constructor, but that isn't important.

DoWork 2
You can implement DoWork by putting your expensive code in it. Then, use the DoWorkEventArgs in its body and as its result. Here we hook up the arguments and results from the RunWorkerAsync call. Note: The TestObject was passed to RunWorkerAsync, and that is received as e.Argument. We also have to cast.
http://www.dotnetperls.com/backgroundworker 7/12

2/21/2014

C# BackgroundWorker Tutorial

E x a m p l et h a ti m p l e m e n t sD o W o r k :C # / / /< s u m m a r y > / / /W h e r ew ed ot h ew o r ki nt h ep r o g r a m( t h ee x p e n s i v es l o ws t u f f ) . / / /< / s u m m a r y > p r i v a t ev o i db a c k g r o u n d W o r k e r 1 _ D o W o r k ( o b j e c ts e n d e r ,D o W o r k E v e n t A r g se ) { / / / /e . A r g u m e n ta l w a y sc o n t a i n sw h a t e v e rw a ss e n tt ot h eb a c k g r o u n dw o r k e r / /i nR u n W o r k e r A s y n c .W ec a ns i m p l yc a s ti tt oi t so r i g i n a lt y p e . / / T e s t O b j e c ta r g u m e n t T e s t=e . A r g u m e n ta sT e s t O b j e c t ; / / / /B o r i n g . . . . / / T h r e a d . S l e e p ( 1 0 0 0 0 ) ; a r g u m e n t T e s t . O n e V a l u e=6 ; a r g u m e n t T e s t . T w o V a l u e=3 ; / / / /N o w ,r e t u r nt h ev a l u e sw eg e n e r a t e di nt h i sm e t h o d . / /. . .U s ee . R e s u l t . / / e . R e s u l t=a r g u m e n t T e s t ;

RunWorkerCompleted
You can use the RunWorkerCompleted event handler by going to the lightning bolt in the designer and clicking on the backgroundWorker1 icon in the tray. Now double-click in RunWorkerCompleted.
http://www.dotnetperls.com/backgroundworker 8/12

2/21/2014

C# BackgroundWorker Tutorial

Next: You will get some autogenerated code that looks just like this. Put the argument receiving code in this method.
E x a m p l et h a ts h o w sR u n W o r k e r C o m p l e t e de v e n th a n d l e r :C # / / /< s u m m a r y > / / /T h i si so nt h em a i nt h r e a d ,s ow ec a nu p d a t eaT e x t B o xo ra n y t h i n g . / / /< / s u m m a r y > p r i v a t ev o i db a c k g r o u n d W o r k e r 1 _ R u n W o r k e r C o m p l e t e d ( o b j e c ts e n d e r , R u n W o r k e r C o m p l e t e d E v e n t A r g se ) { / / / /R e c e i v et h er e s u l tf r o mD o W o r k ,a n dd i s p l a yi t . / / T e s t O b j e c tt e s t=e . R e s u l ta sT e s t O b j e c t ; t h i s . T e x t=t e s t . O n e V a l u e . T o S t r i n g ( )+""+t e s t . T w o V a l u e . T o S t r i n g ( ) ; / / / /W i l ld i s p l a y" 63 "i nt i t l eT e x t( i nt h i se x a m p l e ) / /

Discussion
You probably know more than you think about the BackgroundWorker class. BackgroundWorker has a name that might indicate it is more complex than it really is. There are many more details about threading and abort calls.
http://www.dotnetperls.com/backgroundworker 9/12

2/21/2014

C# BackgroundWorker Tutorial

However: Once you understand that BackgroundWorker is just a structural overlay to threads in Windows Forms, it is intuitive. First, call RunWorkerAsync with an argument. You can pass any argument to this method on BackgroundWorker, including null. It simply must inherit from object, which everything does. Second: Custom processing is run. Your expensive code is executed in the DoWork method. Insert pause here as your program works. Third: It finishes. When your processing is done, RunWorkerCompleted is called. In this method, you receive the result.

ProgressBar
A ProgressBar visually displays progress. And the ProgressBar control in Windows Forms can be used with a BackgroundWorker. This site has a detailed tutorial on how to use these controls together.
http://www.dotnetperls.com/backgroundworker 10/12

2/21/2014

C# BackgroundWorker Tutorial

ProgressBar Also: The WPF ProgressBar is covered. It too can be used with a BackgroundWorker control. ProgressBar

Summary
We implemented the code for a BackgroundWorker control. We generally instead prefer ThreadPool when we need many threads. And threads are not always useful. IO operations, for example, cannot always be multithreaded well. Thus: With the era of multiple processor systems, we need threads. And BackgroundWorker is an excellent shortcut.

C# Testing C# Programming Tutorial Test C# Code


http://www.dotnetperls.com/backgroundworker 11/12

2/21/2014

C# BackgroundWorker Tutorial

C# Forum
www.daniweb.com Free Answers to Your Programming Questions from our Savvy Community

http://www.dotnetperls.com/backgroundworker

12/12

Das könnte Ihnen auch gefallen