Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Framework 2.0  »  Multithreading The Easy Way: The BackgroundWorker
 »  Home  »  .NET Framework  »  Framework 3.0  »  Multithreading The Easy Way: The BackgroundWorker
 »  Home  »  .NET Intermediate  »  Multithreading The Easy Way: The BackgroundWorker
 »  Home  »  Visual Studio 2005  »  Multithreading The Easy Way: The BackgroundWorker
 »  Home  »  Visual Studio 2008  »  Multithreading The Easy Way: The BackgroundWorker
 »  Home  »  Windows Development  »  Visual Basic 2005  »  Multithreading The Easy Way: The BackgroundWorker
 »  Home  »  Windows Development  »  Windows Presentation Foundation  »  Multithreading The Easy Way: The BackgroundWorker
Multithreading The Easy Way: The BackgroundWorker
by Ged Mead | Published  06/01/2008 | Framework 2.0 Framework 3.0 .NET Intermediate Visual Studio 2005 Visual Studio 2008 Visual Basic 2005 Windows Presentation Foundation | Rating:
Ged Mead

Ged Mead (XTab) is a Microsoft Visual Basic MVP who has been working on computer software and design for more than 25 years. His journey has taken him through many different facets of IT. These include training as a Systems Analyst, working in a mainframe software development environment, creating financial management systems and a short time spent on military laptop systems in the days when it took two strong men to carry a 'mobile' system.

Based in an idyllic lochside location in the West of Scotland, he is currently involved in an ever-widening range of VB.NET, WPF and Silverlight development projects. Now working in a consultancy environment, his passion however still remains helping students and professional developers to take advantage of the ever increasing range of sophisticated tools available to them.

Ged is a regular contributor to forums on vbCity and authors articles for DevCity. He is a moderator on VBCity and the MSDN Tech Forums and spends a lot of time answering technical questions there and in several other VB forum sites. Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, and DevCity.NET Newsletter Editor. He has written and continues to tutor a number of free online courses for VB.NET developers.


View all articles by Ged Mead...
The Task

  This article came about because I had seen several posts on vbCity recently where I thought the best approach might be to use a BackgroundWorker.  As you probably know, this is a helper component that wraps away some of the down-and-dirty stuff you need if you want to use multithreading. The BackgroundWorker won't meet every possible scenario where you have to get stuck into multithreading, but it's a very useful little tool in many basic situations.

  For this example I am going to run a slow, somewhat resource intensive process on a background thread and will test that it is working as it should by making another form containing other controls available to the user while the slow background task is running.

  The actual task is a (kinda) real world one where the code makes a search for specific files  in directories on a user's hard drive.   Just for interest I have extended this basic scenario so that files that match a particular file type (e.g. text files) are further searched to see if they contain a specific string.

  This makes it a potentially long running task and therefore one that is very suitable to carry out on a separate asynchronous thread; the result being that the user can continue to use other parts of the application without the whole thing hanging while the searches take place.

  The code below is the skeleton of the background task:  

Code Copy
  Sub FileFinder(ByVal dir As String)
            ' Display all files in a directory that match file type
            For Each fname As String In Directory.GetFiles(dir)
        If fname.EndsWith("txt") Then
                    'TODO:  pass back progress message
              End If
           ' A recursive call for all the subdirectories in this directory.
            For Each subdir As String In Directory.GetDirectories(dir)
        Catch ex As Exception
        End Try
    End Sub

Sponsored Links