Developing Long Running Tasks Using Asynchronous Programming with VB.NET 2005 - The asynchronous way of dealing with a long task
(Page 4 of 4 )
This is a very complicated method when you compare it with the method given in the previous section. But it is essential for certain types of applications.
To deal with the asynchronous method, you should be familiar with threading, eventing and delegating. If you are very new to delegating, I request you go through the “events and delegates” information on MSDN before proceeding with the code. The following is the entire code, which you can use as a template for your own long-running applications:
Dim thExecuteTaskAsync As Thread = Nothing
Private Sub StartExecuteTaskAsync()
'clear existing thread
If Not thExecuteTaskAsync Is Nothing Then
thExecuteTaskAsync.Abort()
thExecuteTaskAsync.Join()
thExecuteTaskAsync = Nothing
End If
'start a new thread to execute the task asynchronously
thExecuteTaskAsync = New Thread(AddressOf
ExecuteTaskAsync)
thExecuteTaskAsync.Start()
End Sub
Private Sub ExecuteTaskAsync()
For i As Long = 1 To 100000
'access delegate to show status on GUI
Invoke(ShowStatus, New Object() {CType(i, String)})
Next
End Sub
'================================================================
''DELEGATE declaration
Private Delegate Sub delShowStatus(ByVal i As String)
Dim ShowStatus As New delShowStatus(AddressOf ShowMsg)
Private Sub ShowMsg(ByVal i As String)
Me.lblMessage.Text = i
End Sub
'=============================================================
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e
As System.Windows.Forms.FormClosingEventArgs) Handles
Me.FormClosing
'this is necessary if the form is trying to close, even
before the completion of task
If Not thExecuteTaskAsync Is Nothing Then
thExecuteTaskAsync.Abort()
End Sub
Private Sub btnAsynchronous_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnAsynchronous.Click
StartExecuteTaskAsync()
MsgBox("Finished")
End Sub
In this article, I simply wanted to explain the basic topics of asynchronous programming in VB.NET 2005/2003. The sample codes given in this article may not be suitable for every application. You can neither consider it as the best in performance nor the best in programming methodologies. It is up to you to make the correct decision based on the necessities of the applications being developed.
Even though I developed a simple application to test asynchronous programming for long-running-tasks, you can really develop highly complicated applications in an easy manner with the help of asynchronous programming and its features. In my upcoming articles, I shall add a few more examples covering the same.
If you are new to developing long-running-tasks in ASP.NET web applications, you can refer to another contribution of mine.
Any feedback, suggestions, bugs, errors, improvements etc., are highly appreciated at jag_chat@yahoo.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |