Cannot find Mutex created in win service from win application

R

roman.muntyanu

Hi All,

I have a program that can be started as windows service or as an
windows app.
I need to avoid starting this program as an application if it is
already started as a win service.

In code when program starts as a win service I create named mutex.
Then if user tries to start program as a win application I am checking
if Mutex with the same name exists already and exit program if Mutex
found.

My problem is that Mutex that was created when program started as win
service cannot be found when program starting as a win application.

If I try to run program as a win application twice during second
attempt Mutex is found.

I would appreciate if anyone can point me how to fix this.

Thanks,
Roman
 
M

Marc Gravell

Try starting the mutex with Global\ - i.e. @"Global\Your.Mutex.Name" -
then it should be able to find it.

Marc
 
M

Marc Gravell

Also; one common cause for this [a mutex representing a singleton app
not being found] is actually GC - i.e. your mutex might be getting
collected (hence finalized, hence mutex released) earlier than you
think. Jon covers this here (the second half of the document):

http://www.yoda.arachsys.com/csharp/threads/waithandles.shtml

(in short, consider adding a GC.KeepAlive(mutex) if your code looks
similar...)

Marc
 
R

roman.muntyanu

Try starting the mutex with Global\ - i.e. @"Global\Your.Mutex.Name" -
then it should be able to find it.

Marc

Thanks a lot, Marc
It works now

Roman
 
R

roman.muntyanu

Thanks a lot, Marc
It works now

Roman

Thanks again,
It worked without using GC.KeepAlive(mutex)
But I will add this too to keep mutex alive

Roman
 
J

Jon Skeet [C# MVP]

[Jon; probably not enough payoff, but have you thought of a
permalink-type header that simply writes the pobox address?]

Hmm. I suspect I could tell apache to do that, but it would be a
certain amount of effort to work out where, and it wouldn't just be my
time spent on it (I'd have to ask the Arachsys guys to make the
change). It's possible that a META tag might have the same effect -
not sure.

To be honest, I think it's safe enough to put the yoda URL in posts
etc - it's unlikely I'll be changing provider for quite a long time,
and if I do it'll screw up lots of other people who already link to
yoda...

Jon
 

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

Top