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

How can I check for thread existense ?

P: n/a
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.GetCurrentProce ss.ProcessName).Length
1 Then
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.
Aug 27 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"fniles" <fn****@pfmail.comwrote in
news:#N*************@TK2MSFTNGP05.phx.gbl:
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...

Aug 27 '07 #2

P: n/a
Mutexes are normally used for detecting multiple instances of the same app

For threading you normally use the semaphore type

regards

michel
"Spam Catcher" <sp**********@rogers.comschreef in bericht
news:Xn**********************************@127.0.0. 1...
"fniles" <fn****@pfmail.comwrote in
news:#N*************@TK2MSFTNGP05.phx.gbl:
>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...

Aug 27 '07 #3

P: n/a
Thank you.
What is semaphore type ?
"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:u6**************@TK2MSFTNGP02.phx.gbl...
Mutexes are normally used for detecting multiple instances of the same app

For threading you normally use the semaphore type

regards

michel
"Spam Catcher" <sp**********@rogers.comschreef in bericht
news:Xn**********************************@127.0.0. 1...
>"fniles" <fn****@pfmail.comwrote in
news:#N*************@TK2MSFTNGP05.phx.gbl:
>>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...


Aug 28 '07 #4

P: n/a
And how can I use the semaphore type to detect multiple instances of the
same thread ?

"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:u6**************@TK2MSFTNGP02.phx.gbl...
Mutexes are normally used for detecting multiple instances of the same app

For threading you normally use the semaphore type

regards

michel
"Spam Catcher" <sp**********@rogers.comschreef in bericht
news:Xn**********************************@127.0.0. 1...
>"fniles" <fn****@pfmail.comwrote in
news:#N*************@TK2MSFTNGP05.phx.gbl:
>>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...


Aug 28 '07 #5

P: n/a
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/lib...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

"fniles" <fn****@pfmail.comschreef in bericht
news:e3**************@TK2MSFTNGP06.phx.gbl...
And how can I use the semaphore type to detect multiple instances of the
same thread ?

"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:u6**************@TK2MSFTNGP02.phx.gbl...
>Mutexes are normally used for detecting multiple instances of the same
app

For threading you normally use the semaphore type

regards

michel
"Spam Catcher" <sp**********@rogers.comschreef in bericht
news:Xn**********************************@127.0.0 .1...
>>"fniles" <fn****@pfmail.comwrote in
news:#N*************@TK2MSFTNGP05.phx.gbl:

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...



Aug 28 '07 #6

P: n/a
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 ?
"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:eV**************@TK2MSFTNGP05.phx.gbl...
>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/lib...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

"fniles" <fn****@pfmail.comschreef in bericht
news:e3**************@TK2MSFTNGP06.phx.gbl...
>And how can I use the semaphore type to detect multiple instances of the
same thread ?

"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:u6**************@TK2MSFTNGP02.phx.gbl...
>>Mutexes are normally used for detecting multiple instances of the same
app

For threading you normally use the semaphore type

regards

michel
"Spam Catcher" <sp**********@rogers.comschreef in bericht
news:Xn**********************************@127.0. 0.1...
"fniles" <fn****@pfmail.comwrote in
news:#N*************@TK2MSFTNGP05.phx.gbl:

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...



Aug 29 '07 #7

P: n/a
"fniles" <fn****@pfmail.comwrote in
news:eq**************@TK2MSFTNGP02.phx.gbl:
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
Aug 29 '07 #8

P: n/a
Yes, I can do that. Thank you.
I was thinking to check for it inside the thread itself.
Can I do that ?

"Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
"fniles" <fn****@pfmail.comwrote in
news:eq**************@TK2MSFTNGP02.phx.gbl:
>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

Aug 30 '07 #9

P: n/a

Yes you can do that by using 2 semaphores
"fniles" <fn****@pfmail.comschreef in bericht
news:eq**************@TK2MSFTNGP02.phx.gbl...
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 ?
"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:eV**************@TK2MSFTNGP05.phx.gbl...
>>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/lib...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

"fniles" <fn****@pfmail.comschreef in bericht
news:e3**************@TK2MSFTNGP06.phx.gbl...
>>And how can I use the semaphore type to detect multiple instances of the
same thread ?

"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:u6**************@TK2MSFTNGP02.phx.gbl...
Mutexes are normally used for detecting multiple instances of the same
app

For threading you normally use the semaphore type

regards

michel
"Spam Catcher" <sp**********@rogers.comschreef in bericht
news:Xn**********************************@127.0 .0.1...
"fniles" <fn****@pfmail.comwrote in
news:#N*************@TK2MSFTNGP05.phx.gbl:
>
>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...
>




Aug 31 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.