Link to home
Start Free TrialLog in
Avatar of AUROS
AUROS

asked on

Named Semaphore shared between windows application and service

Hi all,

I'm trying to use a named semaphore in order to trigger a windows service action by an external application.

My windows service code look like this:

        private void Run()
        {
            try
            {
                bool created;
                namedSemaphore = new Semaphore(0, 1, Settings.Default.MutexName, out created);
                if (!created)
                    throw new ApplicationException(string.Format("Named semaphore {0} was already created by another process", Settings.Default.MutexName));
            }
            catch (Exception ex)
            {
                svc.WriteLogEntry(string.Format("Failed creating named semaphore. \r\nException was: \r\n{0}",
                    ex), EventLogEntryType.Error);
            }

            try
            {
                while (true)
                {
                    namedSemaphore.WaitOne();
                    PerformAction();
                }
            }
            catch (ThreadAbortException)
            {
                Thread.ResetAbort();
                workerThread = null;
            }
            catch (Exception ex)
            {
                svc.LogException(ex);
            }
            finally
            {
                namedSemaphore.Close();
                namedSemaphore = null;
            }
        }

Open in new window


The code triggering the action in the windows service looks like this (resides in an external application):

        private void RunTrigger(object arg)
        {
            Semaphore namedSemaphore = null;
            try
            {
                namedSemaphore = new Semaphore(0, 1, "Action");
                namedSemaphore.Release(1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (namedSemaphore != null)
                    namedSemaphore.Close();
            }
        }

Open in new window


My problem is:
When running the service code as a console application everything works.
But when the code runs as a windows service, execution remains blocked in the WaitOne code, and it doesn't respond to the semaphore's trigger from the 2nd application.

Any help would be greatly appreciated.
Thanks.
Avatar of pivar
pivar
Flag of Sweden image

Hi,

Just a thought, is the service logging on as the same user as the console application? Can it be a security issue?

/peter
Avatar of AUROS
AUROS

ASKER

I tried changing the service's log-on user to the same user as the application, but it made no difference...
ASKER CERTIFIED SOLUTION
Avatar of Jesse Houwing
Jesse Houwing
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AUROS

ASKER

Thanks a lot ToAoM - that did the job.