473,385 Members | 2,044 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,385 software developers and data experts.

Using mutex or semaphore

I have an app that is run periodically using Winsows Scheduled Tasks.
Occasionally, the app will still be running by the time the scheduled
task rolls around again.

I don't want the task scheduler to start another instance of my app,
but instead check for an existing instance. If there is already an
instance of the app running, then I want the second instance to signal
the first and then exit.

I know I can use a mutex to check for an existing instance of my app.
But how can I make the second instance signal the first? Would a
semaphore work in this instance?
Thanks for any assistance you can provide

Nov 21 '05 #1
8 5459
Hi,

Try something like this.

Public Sub main()
Dim owned As Boolean
Dim mut As New System.Threading.Mutex(True, "xvcjsdf67AS124#$3",
owned)

If owned Then
Application.Run(New Form1)
mut.ReleaseMutex()
Else
MessageBox.Show("A previous instance is already running")
End If
End Sub

Ken
--------------------------

"Chris" <du******@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I have an app that is run periodically using Winsows Scheduled Tasks.
Occasionally, the app will still be running by the time the scheduled
task rolls around again.

I don't want the task scheduler to start another instance of my app,
but instead check for an existing instance. If there is already an
instance of the app running, then I want the second instance to signal
the first and then exit.

I know I can use a mutex to check for an existing instance of my app.
But how can I make the second instance signal the first? Would a
semaphore work in this instance?
Thanks for any assistance you can provide
Nov 21 '05 #2
Hi,

Try something like this.

Public Sub main()
Dim owned As Boolean
Dim mut As New System.Threading.Mutex(True, "xvcjsdf67AS124#$3",
owned)

If owned Then
Application.Run(New Form1)
mut.ReleaseMutex()
Else
MessageBox.Show("A previous instance is already running")
End If
End Sub

Ken
--------------------------

"Chris" <du******@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I have an app that is run periodically using Winsows Scheduled Tasks.
Occasionally, the app will still be running by the time the scheduled
task rolls around again.

I don't want the task scheduler to start another instance of my app,
but instead check for an existing instance. If there is already an
instance of the app running, then I want the second instance to signal
the first and then exit.

I know I can use a mutex to check for an existing instance of my app.
But how can I make the second instance signal the first? Would a
semaphore work in this instance?
Thanks for any assistance you can provide
Nov 21 '05 #3

The "proper" way would be to use remoting, but that may be overkill
for your needs.

Another in-memory way is to just have the first instance listen on a
tcp port and whenever it receive a message (doesn't really matter
what) it knows to continue. You could send meaningful messages if you
want but it seems that in this case it's not needed.

Another real kludgy way would be to write to a file--if you're talking
about scheduled tasks then checking once per run shouldn't be a big
performance hit. You can either store a numeric counter in a file and
read/write it or you can use the file name itself in a known temp-dir
as a numeric counter.

HTH,

Sam

On 10 Jan 2005 11:53:47 -0800, "Chris" <du******@gmail.com> wrote:
I have an app that is run periodically using Winsows Scheduled Tasks.
Occasionally, the app will still be running by the time the scheduled
task rolls around again.

I don't want the task scheduler to start another instance of my app,
but instead check for an existing instance. If there is already an
instance of the app running, then I want the second instance to signal
the first and then exit.

I know I can use a mutex to check for an existing instance of my app.
But how can I make the second instance signal the first? Would a
semaphore work in this instance?
Thanks for any assistance you can provide


Nov 21 '05 #4

The "proper" way would be to use remoting, but that may be overkill
for your needs.

Another in-memory way is to just have the first instance listen on a
tcp port and whenever it receive a message (doesn't really matter
what) it knows to continue. You could send meaningful messages if you
want but it seems that in this case it's not needed.

Another real kludgy way would be to write to a file--if you're talking
about scheduled tasks then checking once per run shouldn't be a big
performance hit. You can either store a numeric counter in a file and
read/write it or you can use the file name itself in a known temp-dir
as a numeric counter.

HTH,

Sam

On 10 Jan 2005 11:53:47 -0800, "Chris" <du******@gmail.com> wrote:
I have an app that is run periodically using Winsows Scheduled Tasks.
Occasionally, the app will still be running by the time the scheduled
task rolls around again.

I don't want the task scheduler to start another instance of my app,
but instead check for an existing instance. If there is already an
instance of the app running, then I want the second instance to signal
the first and then exit.

I know I can use a mutex to check for an existing instance of my app.
But how can I make the second instance signal the first? Would a
semaphore work in this instance?
Thanks for any assistance you can provide


Nov 21 '05 #5
Ken Tucker [MVP] wrote:
Else
MessageBox.Show("A previous instance is already running")
End If


Thanks Ken,

I understand the part about setting the mutex. What I was asking was
about what the second instance did when it detected the first. In your
example above, instead of showing a message box, I want to send a
signal to the first instance of the app. Something like:

If FilesFoundToProcess then
If Not PriorInstanceRunning then
ProcessFiles
Else
Send signal to prior instance to let it know it has more files
to process
End If
End If

Thanks again,

Chris

Nov 21 '05 #6
Ken Tucker [MVP] wrote:
Else
MessageBox.Show("A previous instance is already running")
End If


Thanks Ken,

I understand the part about setting the mutex. What I was asking was
about what the second instance did when it detected the first. In your
example above, instead of showing a message box, I want to send a
signal to the first instance of the app. Something like:

If FilesFoundToProcess then
If Not PriorInstanceRunning then
ProcessFiles
Else
Send signal to prior instance to let it know it has more files
to process
End If
End If

Thanks again,

Chris

Nov 21 '05 #7
Thanks, I'll check into those ideas.

Chris

Nov 21 '05 #8
Thanks, I'll check into those ideas.

Chris

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Vinay C | last post by:
Hi, Can anyone clear me that, when should we use go for mutex, and in which situation should we opt for monitor, lock, semaphone and other objects, in a multithreaded application for synchronization...
3
by: smith4894 | last post by:
Hello, I have an application that essentially consists of two threads doing their things. One thread is a producer, and pushes bytes (of a struct) into a pipe, and another is a consumer that...
2
by: aBŁ | last post by:
Hello all, I have an application which will seek the sql server table whether it is updated or not. This application runs once in 60 seconds. But the sql server table doesnt get updated...
0
by: Chris | last post by:
I have an app that is run periodically using Winsows Scheduled Tasks. Occasionally, the app will still be running by the time the scheduled task rolls around again. I don't want the task...
6
by: Jun Kusanagi | last post by:
Is there any way to solve process synchronization between more than 1 resources. I'm already trying to use Mutex.WaitAny, but I confused to execute Release method from the corresponding mutexes....
1
by: RupeshDeenadayalan | last post by:
If an OS doesnot support mutex and it only has binary semaphore,Is it possible to acheive mutex concept
1
by: yduani | last post by:
Hi, I'm running 2 different executables: One saving infinitely an excell file, Other Read it infinitely. time duration for Save/Open is randomally. This 2 applications works fine differently,...
1
by: mazzawi | last post by:
I know they can be shared if we use pthread_mutexattr_setpshared with PTHREAD_PROCESS_SHARED but does that only work if the processes are created with a fork, after the mutex has been declared,...
19
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.