Saturday, July 26, 2008

Thread Pool

Instead of creating a thread each time we need one, and then destroying it after finishing, the .NET framework introduces the concept of thread pool. In the thread pool technique, and instead of creating a new thread whenever you need one, your application will obtain a thread from the thread pool. After that obtained thread completes its task, it will be returned to the thread pool instead of destroying it - waiting for the next acquiring. This reusability increases the overall application performance, and reduces the cost of excessive thread creation and termination.

To make use of this technique, you need to use the System.Threading.ThreadPool class. The thread pool will be created the first time you use it. The number of total threads in this pool is restricted by default to 25 threads. This means that all of these 25 threads may be busy at some point. If you need to acquire a new thread at this point, your task will be scheduled waiting for a thread to finish its task and return to the pool.

In the "default.aspx.vb" file, import the "system.Threading" name space as shown in the following line of code.

1
Imports System.Threading

Create a time consuming method that will be executed in background in a separate thread. To be able to assign this method to the thread pool, it must have the same signature as the "WaitCallBack" delegate. This delegate has the following declaration:


Public Delegate Sub WaitCallback(ByVal state As Object)

This delegate represents a callback method to be executed by a thread pool thread. The "state" parameter represents the information that will be used by your time consuming method if you need to pass information to it.

So, our time consuming method will be as indicated in the following code snippet:


17
Private Sub LongTimeTask(ByVal s As Object)
18
19 Dim i As Integer
20 Dim str As String
21 str = s.ToString
22
23 For i = 0 To 1000
24 str = str + "--" + str
25 Next
26
27 End Sub

Now, in the button click handler type the following lines of code:


5
Protected Sub Button1_Click(ByVal sender As _
6 Object, ByVal e As System.EventArgs) Handles Button1.Click
7
8 If ThreadPool.QueueUserWorkItem( _
9 New WaitCallback(AddressOf LongTimeTask), TextBox1.Text) _
10 Then
11 Label2.Text = "Queued successfully"
12 Else
13 Label2.Text = "Failed"
14 End If
15
16 End Sub

In the line #8 of the above code, we used the ThreadPool.QueueUserWorkItem function to queue our time consuming method for execution in a separate thread from the thread pool. The first parameter to this function is a new instance of the WaitCallBack delegate that represents the method to be executed. In our case, the method to be executed is the 'LongTimeTask' method. The second parameter is the parameter to be passed to the 'LongTimeTask' method itself. In our case this parameter is the text entered by the end user in the text box control. This function returns 'True' when the queuing operation succeed, or 'False' when it fails.

Now, run your application and enter any text into the text box, then click the button, and observe the results.

It will be much better to enter long text in the text box, or to increase the number of loop iterations to get better feeling of this operation. You can also add a new text box control to the "default.aspx" page, so you can test your user interface responsiveness while you run the time consuming task at background.

That's it ...

As you can see it is so easy to use this technique to solve your annoying time consuming tasks problem. Be warned then! This ease will cost you.

No comments: