Cannot find Mutex created in win service from win application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • roman.muntyanu@gmail.com

    Cannot find Mutex created in win service from win application

    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
  • Marc Gravell

    #2
    Re: Cannot find Mutex created in win service from win application

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

    Marc

    Comment

    • Marc Gravell

      #3
      Re: Cannot find Mutex created in win service from win application

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



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

      Marc

      Comment

      • roman.muntyanu@gmail.com

        #4
        Re: Cannot find Mutex created in win service from win application

        On May 6, 10:30 am, Marc Gravell <marc.grav...@g mail.comwrote:
        Try starting the mutex with Global\ - i.e. @"Global\Your.M utex.Name" -
        then it should be able to find it.
        >
        Marc
        Thanks a lot, Marc
        It works now

        Roman

        Comment

        • roman.muntyanu@gmail.com

          #5
          Re: Cannot find Mutex created in win service from win application

          On May 6, 10:42 am, roman.munty...@ gmail.com wrote:
          On May 6, 10:30 am, Marc Gravell <marc.grav...@g mail.comwrote:
          >
          Try starting the mutex with Global\ - i.e. @"Global\Your.M utex.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(mu tex)
          But I will add this too to keep mutex alive

          Roman

          Comment

          • Marc Gravell

            #6
            Re: Cannot find Mutex created in win service from win application

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

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

            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


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

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Cannot find Mutex created in win service from win application

              On May 6, 3:50 pm, Marc Gravell <marc.grav...@g mail.comwrote:

              <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

              Comment

              Working...