How can I check for thread existense ?

  • Thread starter Thread starter fniles
  • Start date Start date
F

fniles

I am using VB.NET 2005. To check if the same program already run or not, I
can use the following

If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length
MsgBox("Another instance of " & Process.GetCurrentProcess.ProcessName &
" is already running !")
End
End If

I am wondering if I can check the same thing for a thread.
I have 1 thread that I do not want to create multiple instances of. Can I
check whether the thread has exists before or not ?
Thank you.
 
I am wondering if I can check the same thing for a thread.
I have 1 thread that I do not want to create multiple instances of.
Can I check whether the thread has exists before or not ?
Thank you.

You would use a System Mutex to check if an instance is running. There
should be an example on google (used to be plenty, but I can't seem to find
one!!!)

But VB.NET 2005 already has an option to limit the application to one
instance...
 
Mutexes are normally used for detecting multiple instances of the same app

For threading you normally use the semaphore type

regards

michel
 
Thank you.
What is semaphore type ?


Michel Posseth said:
Mutexes are normally used for detecting multiple instances of the same app

For threading you normally use the semaphore type

regards

michel
 
a thread can attempt to take ownership of the semaphore by calling the
waitone method , if the current count is higher as zero ( count of the
number of threads you allow ) , the semaphore returns inmediatly ,
otherwise it will wait until anoher thread releases the semaphore , or until
the optional timeout expires


here is an MSDN example

http://msdn2.microsoft.com/en-us/library/system.threading.semaphore.aspx

P.s.

remember to use a try finally block to ensure thet the semaphore is released
even if the code throws an exception

Like mutexes semaphores can have a name and be shared among processes when
you try to create a sempahore that already exists the initial and
maximumcounts are ignored


HTH

Michel
 
Thank you.
But, I have 2 different thread running on the application, and I would like
to allow those 2 threads only. They both have a different name, say
"ThreadA" and "ThreadB"
I only want 1 instance of "ThreadA" and 1 instance of "ThreadB"
Can I use semaphore to do that ?
 
But, I have 2 different thread running on the application, and I would
like to allow those 2 threads only. They both have a different name,
say "ThreadA" and "ThreadB"
I only want 1 instance of "ThreadA" and 1 instance of "ThreadB"

Can you check the valud of the Thread Variable?

If ThreadA is Nothing then
'Do stuff here to initialize thread
Else
msgbox("Thread already created")
End if
 
Yes, I can do that. Thank you.
I was thinking to check for it inside the thread itself.
Can I do that ?
 
Yes you can do that by using 2 semaphores


fniles said:
Thank you.
But, I have 2 different thread running on the application, and I would
like to allow those 2 threads only. They both have a different name, say
"ThreadA" and "ThreadB"
I only want 1 instance of "ThreadA" and 1 instance of "ThreadB"
Can I use semaphore to do that ?
 
Back
Top