相关疑难解决方法(0)

Windows服务不断运行

我创建了一个名为ProxyMonitor的Windows服务,我目前处于安装和卸载服务的阶段,就像我想要的那样.

所以我像这样执行应用程序:

C:\\Windows\\Vendor\\ProxyMonitor.exe /install
Run Code Online (Sandbox Code Playgroud)

非常自我解释,然后我得到services.msc并开始服务,但当我这样做时,我收到以下消息:

本地计算机上的代理监视器服务已启动,然后停止.如果没有工作要做,某些服务会自动停止,例如,性能日志和警报服务

我的代码看起来像这样:

public static Main(string[] Args)
{
    if (System.Environment.UserInteractive)
    {
        /*
            * Here I have my install logic
        */
    }
    else
    {
        ServiceBase.Run(new ProxyMonitor());
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在ProxyMonitor类中我有:

public ProxyMonitor()
{
}

protected override void OnStart(string[] args)
{
    base.OnStart(args);
    ProxyEventLog.WriteEntry("ProxyMonitor Started");

    running = true;
    while (running)
    {
        //Execution Loop
    }
}
Run Code Online (Sandbox Code Playgroud)

onStop()我只是改变running变量false;

我需要做些什么来使服务保持活跃,因为我需要监控我需要跟踪变化等的网络.


更新:1

protected override void OnStart(string[] args)
{
     base.OnStart(args);
     ProxyEventLog.WriteEntry("ProxyMonitor Started");

     Thread = new …
Run Code Online (Sandbox Code Playgroud)

c# windows-services

53
推荐指数
2
解决办法
7万
查看次数

可以在ThreadStart方法中使用"async"吗?

我有一个Windows服务使用Thread和SemaphoreSlim每60秒执行一些"工作".

class Daemon
{
    private SemaphoreSlim _semaphore;
    private Thread _thread;

    public void Stop()
    {
        _semaphore.Release();
        _thread.Join();
    }

    public void Start()
    {
        _semaphore = new SemaphoreSlim(0);
        _thread = new Thread(DoWork);
        _thread.Start();
    }

    private void DoWork()
    {
        while (true)
        {
            // Do some work here

            // Wait for 60 seconds, or exit if the Semaphore is released
            if (_semaphore.Wait(60 * 1000))                
            {
                return;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想从中调用异步方法DoWork.为了使用await我必须添加async到的关键字DoWork:

private async void DoWork()
Run Code Online (Sandbox Code Playgroud)
  1. 有什么理由不这样做吗?
  2. 如果DoWork已经在专用线程内运行,那么DoWork是否能够异步运行?

c# async-await

11
推荐指数
1
解决办法
2015
查看次数

标签 统计

c# ×2

async-await ×1

windows-services ×1