By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,699 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,699 IT Pros & Developers. It's quick & easy.

Monitoring Multiple Threads

P: n/a
I am starting a number of threads as follows:

For n = 1 To 10
Dim t As New Thread(New ThreadStart(MyProcedure))
t.Name = "Thread " & Trim(Str(n))
t.Start()
Next n

I have a timer that, everytime it fired needs to check that all my threads
are running. I am having difficulties using the t.IsAlive property because
't' is not unique to one thread. I feel I need something like
't.name("Thread1").IsAlive'. In other words, "Is thread "Thread1" still
alive?

How can I get a unique reference to each thread that I can then use?

Thanks

-Jerry
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
What you need is something that holds the reference to each array that you
create. Try a hashtable:

WARNING: The following code is off the top of my head and it is late,..
Might not compile 100% without a bit of tweaking....

Dim threadList as New HashTable
For n = 1 To 10
Dim t As New Thread(New ThreadStart(MyProcedure))
t.Name = "Thread " & Trim(Str(n))
t.Start()
threadList.Add(t.Name, t)

Next n

Now, you should be able to do something like:

Dim isAlive as Boolean

isAlive = DirectCast(threadList.Item("Thread1"), System.Thread).IsAlive


"Jerry Spence1" <je**********@somewhere.com> wrote in message
news:42***********************@ptn-nntp-reader02.plus.net...
I am starting a number of threads as follows:

For n = 1 To 10
Dim t As New Thread(New ThreadStart(MyProcedure))
t.Name = "Thread " & Trim(Str(n))
t.Start()
Next n

I have a timer that, everytime it fired needs to check that all my threads
are running. I am having difficulties using the t.IsAlive property because
't' is not unique to one thread. I feel I need something like
't.name("Thread1").IsAlive'. In other words, "Is thread "Thread1" still
alive?

How can I get a unique reference to each thread that I can then use?

Thanks

-Jerry

Nov 21 '05 #2

P: n/a
Thanks Ray. Actually I tried this with a collection as follows:
Public runningThreads As New Collection

Dim t As New Thread(New ThreadStart(AddressOf MyProcedure))
t.Name = "ReaderThread" & Trim(Str(n))
t.Start()
runningThreads.Add(t)
Then in my Timer:

For Each t In runningThreads
If t.ThreadState = Threading.ThreadState.Stopped Then
Dim t1 As New Thread(New ThreadStart(MyProcedure))
t1.Name = t.Name
runningThreads.Remove(q)
t1.Start()
runningThreads.Add(t1)
End If
Next

However, I am having a few problems with it. What is the difference between
a collection and a hashtable as far as this problem is concerned?

Thanks

-Jerry
"Ray Cassick (Home)" <rc************@enterprocity.com> wrote in message
news:ut*************@TK2MSFTNGP14.phx.gbl...
What you need is something that holds the reference to each array that you
create. Try a hashtable:

WARNING: The following code is off the top of my head and it is late,..
Might not compile 100% without a bit of tweaking....

Dim threadList as New HashTable
For n = 1 To 10
Dim t As New Thread(New ThreadStart(MyProcedure))
t.Name = "Thread " & Trim(Str(n))
t.Start()
threadList.Add(t.Name, t)

Next n

Now, you should be able to do something like:

Dim isAlive as Boolean

isAlive = DirectCast(threadList.Item("Thread1"), System.Thread).IsAlive


"Jerry Spence1" <je**********@somewhere.com> wrote in message
news:42***********************@ptn-nntp-reader02.plus.net...
I am starting a number of threads as follows:

For n = 1 To 10
Dim t As New Thread(New ThreadStart(MyProcedure))
t.Name = "Thread " & Trim(Str(n))
t.Start()
Next n

I have a timer that, everytime it fired needs to check that all my
threads are running. I am having difficulties using the t.IsAlive
property because 't' is not unique to one thread. I feel I need something
like 't.name("Thread1").IsAlive'. In other words, "Is thread "Thread1"
still alive?

How can I get a unique reference to each thread that I can then use?

Thanks

-Jerry


Nov 21 '05 #3

P: n/a
The hashtable just lets you keep a reference to your object (your thread
instance in this case) indexed by another object (the thread name in my
exmaple) It lets you get the reference by the index without having to loop
through all the items to get to the one you want.

I am curious what problems you were having though...

The line you have that removes the thread if it is not running:

runningThreads.Revove(q)

....where are you getting q from?

What 'problems' were you having...

"Jerry Spence1" <je**********@somewhere.com> wrote in message
news:42*********************@ptn-nntp-reader03.plus.net...
Thanks Ray. Actually I tried this with a collection as follows:
Public runningThreads As New Collection

Dim t As New Thread(New ThreadStart(AddressOf MyProcedure))
t.Name = "ReaderThread" & Trim(Str(n))
t.Start()
runningThreads.Add(t)
Then in my Timer:

For Each t In runningThreads
If t.ThreadState = Threading.ThreadState.Stopped Then
Dim t1 As New Thread(New ThreadStart(MyProcedure))
t1.Name = t.Name
runningThreads.Remove(q)
t1.Start()
runningThreads.Add(t1)
End If
Next

However, I am having a few problems with it. What is the difference between a collection and a hashtable as far as this problem is concerned?

Thanks

-Jerry
"Ray Cassick (Home)" <rc************@enterprocity.com> wrote in message
news:ut*************@TK2MSFTNGP14.phx.gbl...
What you need is something that holds the reference to each array that you create. Try a hashtable:

WARNING: The following code is off the top of my head and it is late,..
Might not compile 100% without a bit of tweaking....

Dim threadList as New HashTable
For n = 1 To 10
Dim t As New Thread(New ThreadStart(MyProcedure))
t.Name = "Thread " & Trim(Str(n))
t.Start()
threadList.Add(t.Name, t)

Next n

Now, you should be able to do something like:

Dim isAlive as Boolean

isAlive = DirectCast(threadList.Item("Thread1"), System.Thread).IsAlive


"Jerry Spence1" <je**********@somewhere.com> wrote in message
news:42***********************@ptn-nntp-reader02.plus.net...
I am starting a number of threads as follows:

For n = 1 To 10
Dim t As New Thread(New ThreadStart(MyProcedure))
t.Name = "Thread " & Trim(Str(n))
t.Start()
Next n

I have a timer that, everytime it fired needs to check that all my
threads are running. I am having difficulties using the t.IsAlive
property because 't' is not unique to one thread. I feel I need something like 't.name("Thread1").IsAlive'. In other words, "Is thread "Thread1"
still alive?

How can I get a unique reference to each thread that I can then use?

Thanks

-Jerry



Nov 21 '05 #4

P: n/a
Thanks for that Ray - cheers

-Jerry
"Ray Cassick" <rc******@nospam.enterprocity.com> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
The hashtable just lets you keep a reference to your object (your thread
instance in this case) indexed by another object (the thread name in my
exmaple) It lets you get the reference by the index without having to loop
through all the items to get to the one you want.

I am curious what problems you were having though...

The line you have that removes the thread if it is not running:

runningThreads.Revove(q)

...where are you getting q from?

What 'problems' were you having...

"Jerry Spence1" <je**********@somewhere.com> wrote in message
news:42*********************@ptn-nntp-reader03.plus.net...
Thanks Ray. Actually I tried this with a collection as follows:
Public runningThreads As New Collection

Dim t As New Thread(New ThreadStart(AddressOf MyProcedure))
t.Name = "ReaderThread" & Trim(Str(n))
t.Start()
runningThreads.Add(t)
Then in my Timer:

For Each t In runningThreads
If t.ThreadState = Threading.ThreadState.Stopped Then
Dim t1 As New Thread(New ThreadStart(MyProcedure))
t1.Name = t.Name
runningThreads.Remove(q)
t1.Start()
runningThreads.Add(t1)
End If
Next

However, I am having a few problems with it. What is the difference

between
a collection and a hashtable as far as this problem is concerned?

Thanks

-Jerry
"Ray Cassick (Home)" <rc************@enterprocity.com> wrote in message
news:ut*************@TK2MSFTNGP14.phx.gbl...
> What you need is something that holds the reference to each array that you > create. Try a hashtable:
>
> WARNING: The following code is off the top of my head and it is late,..
> Might not compile 100% without a bit of tweaking....
>
> Dim threadList as New HashTable
>
>
> For n = 1 To 10
> Dim t As New Thread(New ThreadStart(MyProcedure))
> t.Name = "Thread " & Trim(Str(n))
> t.Start()
> threadList.Add(t.Name, t)
>
> Next n
>
> Now, you should be able to do something like:
>
> Dim isAlive as Boolean
>
> isAlive = DirectCast(threadList.Item("Thread1"), System.Thread).IsAlive
>
>
>
>
> "Jerry Spence1" <je**********@somewhere.com> wrote in message
> news:42***********************@ptn-nntp-reader02.plus.net...
>>I am starting a number of threads as follows:
>>
>> For n = 1 To 10
>> Dim t As New Thread(New ThreadStart(MyProcedure))
>> t.Name = "Thread " & Trim(Str(n))
>> t.Start()
>> Next n
>>
>> I have a timer that, everytime it fired needs to check that all my
>> threads are running. I am having difficulties using the t.IsAlive
>> property because 't' is not unique to one thread. I feel I need something >> like 't.name("Thread1").IsAlive'. In other words, "Is thread "Thread1"
>> still alive?
>>
>> How can I get a unique reference to each thread that I can then use?
>>
>> Thanks
>>
>> -Jerry
>>
>>
>
>



Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.