?
Solved

Named Semaphore shared between windows application and service

Posted on 2011-09-08
6
Medium Priority
?
1,108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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…
Simple Linear Regression
Suggested Courses

765 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