Developing Long Running Tasks Using Asynchronous Programming with VB.NET 2005 - What exactly is the problem?
(Page 2 of 4 )
Selecting a method for displaying/presenting the status to the user is not a problem at all. The problem always lies with the synchronization of the process and presentation.
Sometimes we cannot anticipate how much time the process will take to complete. In such scenarios, it is useless to select a method which shows an expected finishing time. We really need to justify the scenario and possibilities to the best of our abilities. Once the selection is made, we need to implement it with a particular approach.
Now we come to the point of our “approach.” Most developers don’t care about the best and most professional approaches for dealing with the issues of long-running tasks. To be frank, any professional approach is a bit difficult to achieve. But if we make it in the form of a template, it could be reused a number of times.
Let us consider the normal approach for working with a task. I prepared a long-running task as follows:
PrivateSub ExecuteTask()
For i As Long = 1 To 10000000
Me.lblMessage.Text = i
Next
End Sub
To execute the above task, I wrote the following code in a button click event:
PrivateSub btnNormal_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnNormal.Click
ExecuteTask()
MsgBox("Finished")
End Sub
There exists no complication anywhere in any of the above programs. I am showing the status of my task using “lblMessage.” If you execute your application, your application hangs (or doesn’t respond to you) for some time. It only responds when the application has executed the task successfully. Even if you ask it to show you the status, it will not respond in a timely fashion.
How do we deal with this issue professionally? Go through the next section for a solution.
Next: The solution is here >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee