Solved

Named Semaphore shared between windows application and service

Posted on 2011-09-08
6
1,057 Views
Last Modified: 2013-12-04
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
Comment
Question by:AUROS
  • 2
  • 2
6 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 36501499
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
 

Author Comment

by:AUROS
ID: 36501524
I tried changing the service's log-on user to the same user as the application, but it made no difference...
0
 
LVL 17

Accepted Solution

by:
ToAoM earned 500 total points
ID: 36509534
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
 
LVL 17

Expert Comment

by:ToAoM
ID: 36509535
0
 

Author Closing Comment

by:AUROS
ID: 36509838
Thanks a lot ToAoM - that did the job.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now