Threading Question

C

Carly

Hi,

I have the following code:

Imports System.Threading
Module Module1

Sub Main()
Dim mydel As New WaitCallback(AddressOf Showmytext)
ThreadPool.QueueUserWorkItem(mydel)
ThreadPool.QueueUserWorkItem(mydel)
ThreadPool.QueueUserWorkItem(mydel)
Console.Read()



End Sub
Sub Showmytext(ByVal state As Object)
Dim mystr As String = CType(state, String)
Console.Write(Thread.CurrentThread.ManagedThreadId)
Dim i As Integer
Dim j As Long
For i = 1 To 10000
j = 0
j = 10000 * 123456

Next
Thread.Sleep(1000)
Console.Write(j)
End Sub


End Module

All my code runs in the SAME thread and runs... synchronously meaning
it ways for the first for i-----sleeps then writes the result on the
console , moves to the next.... etc.

Please somebody explain why.

Thanks,

Carly
 
L

Lucian Wischik

Carly said:
All my code runs in the SAME thread and runs... synchronously meaning
it ways for the first for i-----sleeps then writes the result on the
console , moves to the next.... etc.
Please somebody explain why.

I just ran your code and it used two threads in the pool.

Then I increased the Sleep to Sleep(10000) and it used three threads
in the pool.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top