Maybe this could appear strange for someone, but every day I can see that
lots of Windows Forms applications are built without taking the multithreading
aspect in the right consideration.
An application can have a good response time for the main part of its work,
but under certain situations it could have a terrible decrease of performances
and become blocked to wait the completion of a time-consuming operation.
Sometimes I can see these aspects on data driven or computational application,
where complex operations on the retrieved data must be performed: if the amount
of retrieved data is high and the operation is complex, the application is
blocked to wait the end of the procedure.
This is terrible for the end user but it's the reality: many applications
lacks on threading capabilities, and for the actual power of our hardware this
is not a good aspect. I know that threading is not always a simple aspect to
understand but now with .NET we've powerful tools to build multithreaded
applications without suffer too much (I remember my degree thesys, when I had to
write a multithreaded application in C language under Linux... terrible!
).
The input for these considerations comes from one of the last articles
appeared on MSDN online: Using
Threading to Build a Responsive Application with Visual Studio
2005.
Visual Studio .NET 2005 helps us more on these aspects by introducing a new
tool to build multithreaded application, the BackroundWorker
component.

We can launch our task (with MyLongTaskParameter as input
parameter) on a thread simply by calling:
backgroundWorker1.RunWorkerAsync(MyLongTaskParameter)
and this will generate an event on the BackgroundWorker called DoWork where
we'll compute the MyLongTask process:
Private Sub backgroundWorker1_DoWork(
_
ByVal sender As Object, _
ByVal e
As DoWorkEventArgs) _
Handles
backgroundWorker1.DoWork
' Get the
BackgroundWorker object that raised this
event.
Dim worker As
System.ComponentModel.BackgroundWorker=
_
CType(sender, System.ComponentModel.BackgroundWorker)
' Assign
the result of the computation
' to
the Result property of the
DoWorkEventArgs
' object. This is
will be available to the
'
RunWorkerCompleted eventhandler.
e.Result = MyLongTask(e.Argument, worker, e)
End
Sub
Our MyLongTask function will be declared as follow:
Function MyLongTask(
_
ByVal MyLongTaskParameter As
Integer, _
ByVal worker As
System.ComponentModel.BackgroundWorker,
_
ByVal e As
System.ComponentModel.DoWorkEventArgs) As Long
Quite simple work for a terrible performance improvements.
