473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How can I check for thread existense ?

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
9 4095
"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
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
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
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
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
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
"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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sameh Ahmed | last post by:
Hello there Is there a way through dotNet to check if a certain user is a member of a specific group? I use ADSI to get the memberships of the user then compare them to the group I want to check,...
6
by: Hermann Maier | last post by:
Hi, i am using the com api of a program and if this program is shut down and my program calls a method of the api, it crashs. that means, i need to check, if the com server is still available,...
0
by: Hermann Maier | last post by:
Hi, i am using the com api of a program and if this program is shut down and my program calls a method of the api, it crashs. that means, i need to check, if the com server is still available,...
5
by: ags5406 | last post by:
I've a Windows Service that keeps a particular executable running. If the executable fails for whatever reason, the Service restarts it. Right now I'm using a loop to check if the process is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.