小编Mik*_*ike的帖子

Topshelf - 处理循环

通常使用服务,您要完成的任务会重复,可能是循环或可能是触发器或其他内容.

我正在使用Topshelf为我完成重复任务,特别是我正在使用Shelf'ing功能.

我遇到的问题是如何处理任务的循环.

当引导捆绑Topshelf中的服务时,你传递一个类(在这种情况下ScheduleQueueService)并指出它的Start方法和Stop方法:

例:

    public class QueueBootstrapper : Bootstrapper<ScheduledQueueService>
{
    public void InitializeHostedService(IServiceConfigurator<ScheduledQueueService> cfg)
    {
        cfg.HowToBuildService(n => new ScheduledQueueService());
        cfg.SetServiceName("ScheduledQueueHandler");
        cfg.WhenStarted(s => s.StartService());
        cfg.WhenStopped(s => s.StopService());
    }
}
Run Code Online (Sandbox Code Playgroud)

但在我的StartService()方法中,我使用while循环来重复我正在运行的任务,但是当我尝试通过Windows服务停止服务时,它无法停止并且我怀疑它,因为该StartService()方法在最初调用时从未结束.

例:

 public class ScheduledQueueService
{
    bool QueueRunning;

    public ScheduledQueueService()
    {
      QueueRunning = false;
     }


    public void StartService()
    {
        QueueRunning = true;

        while(QueueRunning){
                     //do some work
         }
    }

  public void StopService()     
  {
         QueueRunning = false;
  }
}
Run Code Online (Sandbox Code Playgroud)

这样做的更好方法是什么?

  1. 我已经考虑使用.NET …

c# loops topshelf

6
推荐指数
1
解决办法
4836
查看次数

node.js同时运行,还是运行?

男孩和女孩,

我今天一直在搞乱node.js,我似乎无法再现这种并发魔法.

我写了这个相当小的服务器:

var http = require("http");

var server = http.createServer(function(req, res) {

    setTimeout(function() {     

        res.writeHead(200,{"content-type":"text/plain"});
        res.end("Hello world!");

    }, 10000);    

});

server.listen(8000);
Run Code Online (Sandbox Code Playgroud)

但是在同一时间在多个chrome选项卡中运行localhost:8000时有什么奇怪的.好像请求是"排队"的.第一个标签需要10秒,第二个标签需要20秒,第三个标签需要30秒等...

但是当使用Links运行这个例子时,它表现出我的期望(同时处理请求).

PS这似乎发生在Chrome和Firefox中

奇异的

node.js

0
推荐指数
1
解决办法
136
查看次数

标签 统计

c# ×1

loops ×1

node.js ×1

topshelf ×1