[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1155
  • Last Modified:

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.
0
AUROS
Asked:
AUROS
  • 2
  • 2
1 Solution
 
pivarCommented:
Hi,

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

/peter
0
 
AUROSAuthor Commented:
I tried changing the service's log-on user to the same user as the application, but it made no difference...
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
Windows services have been severely limited since Windows XP x64, Vista and 7. They're not really allowed to interact with anything desktoppy by default and as such it has become a lot harder to do scenario's like these.

Do you prefix your Mutex name with "Global\" that should make it available across User Sessions, using any other prefix or name results in a Mutex that is only available in the current session.

You could also try activating the "Interact with Desktop" option for your service, but remember that this option is likely to disappear in the next version of windows.

A different supported solution would be to use WCF over Named Pipes for inter process communication. Just set up a WCF Service inside your windows service and listen for incoming requests. Your client can then connect to that.
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
0
 
AUROSAuthor Commented:
Thanks a lot ToAoM - that did the job.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now