打开与服务共享的互斥锁

时间:2013-04-29 14:01:31

标签: c# multithreading service mutex

我有一个服务,它创建一个带有循环的线程,该循环应该运行,直到另一个进程发出互斥锁信号。我的服务代码中有以下内容

        private readonly Mutex _applicationRunning = new Mutex(false, @"Global\HsteMaintenanceRunning");

        protected override void OnStart(string[] args)
        {
            new Thread(x => StartRunningThread()).Start();
        }

        internal void StartRunningThread()
        {
            while (_applicationRunning.WaitOne(1000))
            {
                FileTidyUp.DeleteExpiredFile();    
                _applicationRunning.ReleaseMutex();
                Thread.Sleep(1000);
            }

        }

现在我有一个控制台应用程序应该声明互斥锁并强制退出while循环

        var applicationRunning = Mutex.OpenExisting(@"Global\HsteMaintenanceRunning");
        if (applicationRunning.WaitOne(15000))
        {
            Console.Write("Stopping");
            applicationRunning.ReleaseMutex();
            Thread.Sleep(10000);
        }

当控制台应用程序尝试打开互斥锁时,我收到错误“由于放弃的互斥锁导致等待完成”。这有什么不对吗?

1 个答案:

答案 0 :(得分:1)

我建议您使用服务的内置停止信号而不是互斥锁。互斥类更适合管理对共享资源的独占访问,这不是这里发生的事情。您也可以使用系统事件,但由于服务已经停止时已经有内置的信号机制,为什么不使用它呢?

您的服务代码如下所示:

bool _stopping = false;
Thread _backgroundThread;
protected override void OnStart(string[] args)
{
    _backgroundThread = new Thread(x => StartRunningThread());
    _backgroundThread.Start();
}
protected override void OnStop()
{
    _stopping = true;
    _backgroundThread.Join(); // wait for background thread to exit
}
internal void StartRunningThread()
{
    while (!stopping)
    {
        FileTidyUp.DeleteExpiredFile();
        Thread.Sleep(1000);
    }
}

然后,您的控制台应用程序需要使用框架的ServiceController类将关闭消息发送到您的服务:

using System.ServiceProcess;
...
using (var controller = new ServiceController("myservicename")) {
    controller.Stop();
    controller.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(15.0));
}