c# - named system mutex not recognized -


i trying named system mutex approach synchronize 2 processes-

  1. a c# windows service
  2. a desktop c# app

when mutex created, process didn't create mutex doesn't seem detect existing mutex. below in more detail:

windows service responsible creating mutex(no prefixes-global/local etc. normal named system mutex) follows:

        mutex mymutex= null;         try         {             mymutex= mutex.openexisting(mymutexname);          }         catch (waithandlecannotbeopenedexception x)         {             //doesn't exist.              try             {                 this.getlogger().info("create mutex");                 bool creatednew = false;                 new mutex(false, mymutexname, out creatednew);                 if (!creatednew) throw new exception("unable create mutex");              }             catch (exception ex)             {                 this.stop();             }          }         catch (exception x)         {             this.stop();         }                 {              if (mymutex!= null)             {                 try                 {                     mymutex.releasemutex();                 }                 catch(exception x)                 {                     this.getlogger().warn("unable release mutex");                 }             }         } 

i have in onstart(). also, elsewhere in service synchronize on mutex.

now, in application use thus:

            mutex mymutex= null;          try         {             mymutex= mutex.openexisting(this.mymutexname);             mymutex.waitone(); //do required stuff           }         catch (waithandlecannotbeopenedexception wcbox)         {             //doesn't exist yet. service not running??             this.getlogger().error("background service not running");             shutdownapp();         }                 {             if (mymutex!= null)             {                 try                 {                     mymutex.releasemutex();                 }                 catch (exception x)                 {                 }             }         } 

my issues:

  1. with service running, app throws waithandlecannotbeopenedexception means doesn't see mutex exists (i verified using tool "winobj", mutex exists under "basenamedobjects" type "mutant"). why? run service under same account logged in. without, shouldn't issue because named mutex operating system wide object right?

  2. i not clear on when mutex destroyed. currently, see mutex gets created when start service , when stop service, see mutex no longer exists(winobj). mean mutex garbage collected when there no 1 waiting on , when creator process ends?

  3. in general, life cycle of named system mutex. specifically, when die?

since want share mutex across sessions must use either global namespace, or create own private namespace.

as stands, not specifying namespace. , whilst service defaults use global namespace, interactive application using local namespace.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -