This article explains how to download's file using multi threading. Let's discuss about threading in brief? Whether you are developing for computers with one processor or several, you want your application to provide the most responsive interaction with the user, even if the application is currently doing other work. Using multiple threads of execution is one of the most powerful ways to keep your application responsive to the user and at the same time make use of the processor in between or even during user events. While this section introduces the basic concepts of threading, it focuses on managed threading concepts and using managed threading.
Let's start now first of all create a new window application.
' Threading namespace
Imports System.Threading
' Local variable declarations
Protected WebClient1 As System.Net.WebClient
Protected workerThread As Thread
Here is my start download button click event code:
''' <summary>
''' Start Button click event handler
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click
' Update Status
'StatusListBox.Items.Add("Download Started.")
' Start worker thread that will call Download and Process methods
Try
workerThread = New Thread(New ThreadStart(AddressOf DownloadAndProcessFiles))
workerThread.Start()
Catch ex As Exception
Dim str As String = ex.Message
Finally
'' If there is a problem
'If workerThread.IsAlive Then
' workerThread.Abort()
'End If
End Try
' Update status
End Sub
Here is download functions
Private Sub DownloadAndProcessFiles()
Dim WebClient1 As New System.Net.WebClient
If StatusListBox.InvokeRequired Then
StatusListBox.Invoke(New MethodInvoker(AddressOf DownloadAndProcessFiles), New Object() {})
Exit Sub
End If
StatusListBox.Items.Add("DownloadTest 1 Started")
Try
Thread.Sleep(5000)
WebClient1.DownloadFile("http://www.c-sharpcorner.com/", "C:\Abc.txt")
Catch ex As Exception
Dim str As String
str = ex.Message
End Try
StatusListBox.Items.Add("DownloadTest 1 Done")
StatusListBox.Items.Add("Processing Finished.")
End Sub
This function will download c-sharpcorner home page source in ABC.txt in C drive, you change whatever name you want.
This is form load event
''' <summary>
''' This event is fired when form is loading
''' Initialize your variables here
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
StatusListBox.Items.Add("Initializing Variables.")
InitializeVariables()
StatusListBox.Items.Add("Variables Initialized.")
End Sub
This is form closing event
''' <summary>
''' This event is fired just before the form closes.
''' You want to deinitialize all of your variables
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Form1_FormClosing(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
StatusListBox.Items.Add("Releasing Variables.")
ReleaseVariables()
StatusListBox.Items.Add("Variables Realsed.")
End Sub
''' <summary>
''' Initialize all variables
''' </summary>
''' <remarks></remarks>
Private Sub InitializeVariables()
Me.WebClient1 = New System.Net.WebClient
End Sub
''' <summary>
''' DeInitialize all variables
''' </summary>
''' <remarks></remarks>
Private Sub ReleaseVariables()
If Not Me.WebClient1 Is Nothing Then
Me.WebClient1 = Nothing
End If
End Sub
Output will look like this:
Figure 1.
After click on Start button.
Figure 2.
Check your C drive there should be a ABC.txt file downloaded which has c-sharp corner homepage html source.