Using BackgroundWorker component in a WPF application

by Sumant 24. April 2008 13:57

To use BackgroundWorker component in a WPF application is as easy as using it in a WinForm 2.0 application. The only thing you don't get is the designer support, but we don't need that anyway.

In a WPF application, to create a BackgroundWorker enabled class (or window or control) do following -

  • Import System.ComponentModel namespace
  • Add BackgroundWorker component to your class by declaring it in your class as -

    Visual Basic-Code: Declaring BackgroundWorker component
    	Imports System.ComponentModel
    	
    	Class Test
    	    
    	     Private WithEvents MyBackgroundWorker As New BackgroundWorker 
    	
    	     '... other code ...'
    	
    	End Class
    	
  • Configure its properties in the class initialization method (contructor or loaded event).  

    Visual Basic-Code: Initializing BackgroundWorker
    	Sub New()
    	    MyBackgroundWorker.WorkerSupportsCancellation = False
    	    ' enable / disable Cancellation support
    	    MyBackgroundWorker.WorkerReportsProgress = False
    	    ' enable / disable Progress Reporting support 
    	    ' etc.
    	End Sub
    	

  • Now hook BackgroundWorker events -

    Visual Basic-Code: Hooking BackgroundWorker Events
    	Private Sub MyBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles MyBackgroundWorker.DoWork 
    	
    	        ' your method call / processing here 
    	  
    	End Sub
    	

  • Similalry hook up other events as per your need. If you are using Visual Studio, you can select the same from Control/Event dropdowns in the code view.

Yeah, it's that easy!


 

Share   
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags: ,

.NET | WPF

Windows Forms Controls and Equivalent WPF Controls

by Sumant 24. January 2008 18:12

http://msdn2.microsoft.com/en-us/library/ms750559.aspx

Share   
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags: ,

WPF

© 2008 Sumant Dubey

About this blog

Absolutely technical! The posts are about concepts, tricks, articles and links on technical subjects / problems which are not very obvious to understand / resolve. Purpose is to have this as the first-aid in the time of need.

Disclaimer

The opinions expressed on this website are my own personal opinions and do not represent my employer's view in anyway.