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

Re: Cannot find Mutex created in win service from win application by Marc

Marc
Tue May 06 09:30:33 CDT 2008

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

Marc

Re: Cannot find Mutex created in win service from win application by Marc

Marc
Tue May 06 09:34:09 CDT 2008

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

Re: Cannot find Mutex created in win service from win application by roman

roman
Tue May 06 09:42:58 CDT 2008

On May 6, 10:30=A0am, Marc Gravell <marc.grav...@gmail.com> wrote:
> 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

Re: Cannot find Mutex created in win service from win application by roman

roman
Tue May 06 09:49:05 CDT 2008

On May 6, 10:42=A0am, roman.munty...@gmail.com wrote:
> On May 6, 10:30=A0am, Marc Gravell <marc.grav...@gmail.com> wrote:
>
> > 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

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

Roman

Re: Cannot find Mutex created in win service from win application by Marc

Marc
Tue May 06 09:50:54 CDT 2008

[just noticed you're all sorted, which is great; more for Jon, then...]

Darn redirect... the preferred URL (pobox):

http://www.pobox.com/~skeet/csharp/threads/waithandles.shtml

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

Re: Cannot find Mutex created in win service from win application by Jon

Jon
Tue May 06 11:38:06 CDT 2008

On May 6, 3:50 pm, Marc Gravell <marc.grav...@gmail.com> wrote:

<snip>

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