Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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# 赢得';不能运行多个服务_C#_.net_Windows Services - Fatal编程技术网

C# 赢得';不能运行多个服务

C# 赢得';不能运行多个服务,c#,.net,windows-services,C#,.net,Windows Services,我在Visual Studio 2012中创建了一个Windows服务,该服务在执行时应运行2个服务 static void Main() { // creates an array to hold all services that will run in this application ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] {

我在Visual Studio 2012中创建了一个Windows服务,该服务在执行时应运行2个服务

static void Main()
    {
        // creates an array to hold all services that will run in this application
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        {
            // services added here
            new Service1(),
            new Service2()
        };
        ServiceBase.Run(ServicesToRun);
    }
我已经在各种论坛上阅读了各种可能的答案,但我还没有找到一个有效的解决方案

我为每个服务添加了一个服务安装程序,我还有一个项目安装程序

当我通过开发者命令提示符安装服务时,我可以在计算机管理器中看到Windows服务,但也可以看到Service1和Service2

当我运行Windows服务时,它将只运行Service1而不运行Service2。但是,Service1和Service2都可以单独启动

有什么建议吗?我已经在这上面呆了一段时间了

编辑

在Service1和Service2 OnStart()中,我有以下代码:

CheckService();

        try
        {
            motorTimer = new Timer();
            // timer sets intervals for when quotes are checked after start up
            motorTimer .Elapsed += new ElapsedEventHandler(QuoteTimerElapsed);
            motorTimer .Interval = Convert.ToDouble(ConfigurationSettings.AppSettings["ServiceCheckInterval"]);
            motorTimer .Start();
        }
        catch (Exception ex)
        {
            MotorServiceCheckLogDetail(ex.ToString());
            OnStop();
        }

我已经听说/读到人们在windows服务中使用计时器时面临的一些问题。我建议不要在OnStart方法中使用计时器,而是启动一个使用集成延迟的循环的任务。大致如下:

Task _task;
CancellationTokenSource _terminator;

protected override void OnStart()
{
     _terminator = new CancellationTokenSource();
     _task = Task.Factory.StartNew(TaskBody, _terminator.Token);
}

private void TaskBody(object arg)
{
     var ct = arg as CancellationToken;

     if (ct == null) throw new ArgumentException();

     while(!ct.sCancellationRequested)
     {
         YourOnTimerMethod();
         ct.WaitHandle.WaitOne(interval)
     }
}

public override void OnClose()
{
    _terminator.Cancel();
    _task.Wait();
}

作为对我问题的回答,我采取了以下方法。这可能并不完美,我稍微改变了我的方法,但这是一种让我的服务按我想要的方式工作的方式

我没有创建一个包含多个“服务”的服务,而是创建了一个实例化各种类的服务。我能够在这些类中包含所需的功能,并在需要时调用该类


正如我所说,这并不是我最初问题的完美答案,但这是我发现的一个解决方法,因此,如果其他人阅读这篇文章,这可能会对他们有所帮助。

我也有同样的问题。似乎只有在传递给ServiceBase的第一个服务上才调用OnStart方法。运行。

我曾经遇到过类似的问题,然后决定将其拆分为多个服务,而不是将所有“子服务”放在一个大的服务中。OnStart中有什么?方法实际上,可能更有趣的是,Service1中的OnStart()方法中有什么?如果它没有及时返回,我想服务管理器会放弃启动以下服务。@YaugenVlasau我在OnStart()中有代码,应该在该服务启动时运行。我应该添加一些东西将其链接到Service1吗?Windows服务初学者的一个常见错误是没有意识到服务的工作不能在OnStart()方法或它调用的任何方法中完成。相反,OnStart()应该只启动一个线程来完成这项工作,然后尽快返回。只是尝试了这个修复程序,它仍然没有运行第二个服务。不知道下一步该做什么…建议。。。确保您的2个服务的onStart以我上面建议的方式实现。注释YourOnTimeMethod()的当前实现,并启用一些非常简单的功能(例如在日志中写入值)。如果两个服务都在写入它们的值,请检查您的日志。您认为service1是否也应该有此实现?明确地说。这两个服务的OnStart方法中都应该有一个线程开始。它仅在收到启动请求的服务上调用。这可能是列表中的第一个,但不一定是。服务传递到
Servicebase.Run的顺序与此无关。