none
what is windows service self-aware RRS feed

  • Question

  • Hi

    How to make windows service self-aware

    I tried single ton pattern but no use

    Issue is :http://social.msdn.microsoft.com/Forums/office/en-US/5031074e-bab7-4659-b4b3-909a7d32ec6f/exchange-web-service-error-while-moving-mail-to-other-folder?forum=exchangesvrdevelopment 

    Regards,

    Jagdish


    Jagdish

    Tuesday, December 10, 2013 7:12 AM

Answers

  • I got the Answer 

    Threading with Mutex (Mutual exclusion):

    When two or more threads need to access a shared resource at the same time, the system needs a synchronization mechanism to ensure that only one thread at a time uses the resource. Mutex is a synchronization primitive that grants exclusive access to the shared resource to only one thread. If a thread acquires a mutex, the second thread that wants to acquire that mutex is suspended until the first thread releases the mutex.

    Steps:

    • Instantiating a new Mutex object that's accessible from each thread.
    • Wrapping whatever code you want to be executed in the critical section with that object's WaitOne() and ReleaseMutex() methods in each thread

    Implementation:

    • Add class SingleGlobalInstance to your project

    1. class SingleGlobalInstance : IDisposable
    2. {
    3.     public bool hasHandle = false;
    4.     Mutex mutex;
    5.     private void InitMutex()
    6.     {
    7.         string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();
    8.         string mutexId = string.Format("Global\\{{{0}}}", appGuid);
    9.         mutex = new Mutex(false, mutexId);
    10.         var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
    11.         var securitySettings = new MutexSecurity();
    12.         securitySettings.AddAccessRule(allowEveryoneRule);
    13.         mutex.SetAccessControl(securitySettings);
    14.     }
    15.     public SingleGlobalInstance(int TimeOut)
    16.     {
    17.         InitMutex();
    18.         try
    19.         {
    20.             if(TimeOut <= 0)
    21.                 hasHandle = mutex.WaitOne(Timeout.Infinite, false);
    22.             else
    23.                 hasHandle = mutex.WaitOne(TimeOut, false);
    24.             if (hasHandle == false)
    25.                 throw new TimeoutException("Timeout waiting for exclusive access on SingleInstance");
    26.         }
    27.         catch (AbandonedMutexException)
    28.         {
    29.             hasHandle = true;
    30.         }
    31.     }
    32.     public void Dispose()
    33.     {
    34.         if (mutex != null)
    35.         {
    36.             if (hasHandle)
    37.                 mutex.ReleaseMutex();
    38.             mutex.Dispose();
    39.         }
    40.     }
    41. }
      • Add following condition to check whether thread is already running

    using (new SingleGlobalInstance(1000))

    {

       //code which should be executed in single thread

     }

    Regards,

    Jagdish Kotian


    Jagdish

    • Marked as answer by Jagdish Kotian Tuesday, December 24, 2013 8:42 AM
    Tuesday, December 24, 2013 8:41 AM