Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#windows服务可以';停不下来_C#_Windows Services - Fatal编程技术网

C#windows服务可以';停不下来

C#windows服务可以';停不下来,c#,windows-services,C#,Windows Services,我在尝试停止服务时收到“服务此时无法接受控制消息”。我真的不明白为什么会这样。有人能帮我吗?我以为我的锁会阻止这一切 巡逻法大约需要30-40秒才能运行 private static readonly string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"]; private static readonly int runMinInterval = Convert.ToInt32(Configur

我在尝试停止服务时收到“服务此时无法接受控制消息”。我真的不明白为什么会这样。有人能帮我吗?我以为我的锁会阻止这一切

巡逻法大约需要30-40秒才能运行

private static readonly string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
private static readonly int runMinInterval = Convert.ToInt32(ConfigurationManager.AppSettings["RunMinInterval"]);
private Object myLock = new Object();
private Timer timer;

public DealWatchdogService()
{
    InitializeComponent();
}

protected override void OnStart(string[] args)
{
    Watchdog.Patrol(ConnectionString, new DealWatchdogService());
    timer = new Timer();
    timer.Enabled = true;
    timer.Interval = 60000 * runMinInterval;
    timer.AutoReset = false;
    timer.Start();
    timer.Elapsed += timer_Elapsed;
}

protected override void OnStop()
{
    lock (myLock)
    {
        timer.Stop();
        timer = null;
        base.OnStop();
    }
}

private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    lock (myLock)
    {
         if (timer == null)
                return;
         Watchdog.Patrol(tycheConnection, new DealWatchdogService());
         timer.Start();
    }
}
编辑:可能有一些帮助,但服务有时会很好地停止,但当它已经启动并运行了一周左右时,我会发现这个错误

巡逻法大约需要30-40秒才能运行

private static readonly string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
private static readonly int runMinInterval = Convert.ToInt32(ConfigurationManager.AppSettings["RunMinInterval"]);
private Object myLock = new Object();
private Timer timer;

public DealWatchdogService()
{
    InitializeComponent();
}

protected override void OnStart(string[] args)
{
    Watchdog.Patrol(ConnectionString, new DealWatchdogService());
    timer = new Timer();
    timer.Enabled = true;
    timer.Interval = 60000 * runMinInterval;
    timer.AutoReset = false;
    timer.Start();
    timer.Elapsed += timer_Elapsed;
}

protected override void OnStop()
{
    lock (myLock)
    {
        timer.Stop();
        timer = null;
        base.OnStop();
    }
}

private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    lock (myLock)
    {
         if (timer == null)
                return;
         Watchdog.Patrol(tycheConnection, new DealWatchdogService());
         timer.Start();
    }
}
我认为问题可能是OnStart()花费的时间太长,因为Patrol需要很长时间才能完成,并且正在设置一个标志,表明系统已锁定。将启动代码放在后台线程中,以便OnStart可以更快地完成

protected override void OnStart(string[] args)
{
    //This line does not block.
    Task.Run(() => RealStart());
}

private void RealStart()
{
    Watchdog.Patrol(ConnectionString, new DealWatchdogService());
    timer = new Timer();
    //timer.Enabled = true; //Calling .Start() has the same effect.
    timer.Interval = 60000 * runMinInterval;
    timer.AutoReset = false;
    timer.Elapsed += timer_Elapsed;
    timer.Start(); //Start should be called after you have set .Elapsed
}

唯一的另一件事可能是
timer\u expressed
内部出现问题,但您从未向我们展示该代码在做什么。

如果是
System.Timers.timer
,则不应同时使用和
Start
:“将Enabled设置为true与调用Start相同”(在配置所需的时间间隔之前,您可能不应该启用它)。可能不是您当前的问题,但值得记住。是的,它是System.Timers.Timer。我将对此进行查看并进行更改。您是否在除OnStop()之外的任何地方使用
myLock
?如果锁没有提供任何好处,一次只有一个线程会调用OnStop(),它也在计时器内使用。是否已使用更多的codeCorrect me如果我错了,但不是只有在服务启动时才调用OnStart?服务启动正常。但OnStart()是否正常还是阻塞了吗?@krillezz是的,但我看到当OnStart()内部出现问题时,服务会进入一种奇怪的状态。