Solved

Named Semaphore shared between windows application and service

Posted on 2011-09-08
6
1,062 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:
Jesse Houwing 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:Jesse Houwing
ID: 36509535
0
 

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

24 Experts available now in Live!

Get 1:1 Help Now