Using mutex or semaphore

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

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

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
 
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
 
Ken said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top