Running a code on separate thread or Background Worker | VB.NET

A little background:

When a hard task comes for a VB.NET application, for example Downloading data off Internet, copying large files, etc.. The application hangs over there without making use of more threads. This hanging problem can be solved to a great extent by using the .NET component called ‘BackgroundWorker’.

The little example below will show you how to use it.

  • Drag ‘BackgroundWorker’ from toolbox.
  • Drag a button

Double click on form and type:

Public Shared Sub Thread()
Dim dlURL As String
dlURL = http://example.com/filename.png”

Dim savingpath As String
savingpath = My.Computer.FileSystem.CurrentDirectory.ToString  + “file.png”)

My.Computer.Network.DownloadFile(dlURL, savingpath)
End Sub

Now double click on button and type

BackgroundWorker1.RunWorkerAsync()

What will this example do?

Hmm, it will download the specified file and you will be able to clearly observe that the application does not hang. Run this application and check Network Statistics, the load will be ‘maximum’.

Advertisements

One comment on “Running a code on separate thread or Background Worker | VB.NET

  1. […] Posted in: Programming ← Why do hair colours differ? Running a code on separate thread or Background Worker | VB.NET → LikeBe the first to like this post. Be the first to start a […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s