c# - Windows服务持续运行

标签 c# windows-services

我已经创建了一个名为 ProxyMonitor 的 Windows 服务,目前我正处于按照我想要的方式安装和卸载该服务的阶段。

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

C:\\Windows\\Vendor\\ProxyMonitor.exe /install

非常 self 解释,然后我进入 services.msc 并启动服务,但是当我这样做时,我收到以下消息:

The Proxy Monitor Service on Local Computer started and then stopped. Some services stop automatically if there is no work to do, For example, The performance Logs and Alerts Services

我的代码是这样的:

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

然后在 ProxyMonitor 类中我有:

public ProxyMonitor()
{
}

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

    running = true;
    while (running)
    {
        //Execution Loop
    }
}

onStop() 我只是将running 变量更改为false

我需要做什么才能使该服务始终处于事件状态,因为我需要监视网络我需要跟踪更改等。


更新:1

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

     Thread = new Thread(ThreadWorker);
     Thread.Start();
 }

ThreadWorker 中,我有 ProxyEventLogger.WriteEntry("Main thread entered") 不会被触发。

最佳答案

OnStart() 回调需要及时返回,因此您需要启动一个将执行所有工作的线程。我建议将以下字段添加到您的类(class)中:

using System.Threading;
private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
private Thread _thread;

_thread 字段将保存对您在 OnStart() 回调中创建的 System.Threading.Thread 对象的引用。 _shutdownEvent 字段包含一个系统级事件构造,该构造将用于向线程发出信号以在服务关闭时停止运行。

OnStart() 回调中,创建并启动您的线程。

protected override void OnStart(string[] args)
{
     _thread = new Thread(WorkerThreadFunc);
     _thread.Name = "My Worker Thread";
     _thread.IsBackground = true;
     _thread.Start();
}

您需要一个名为 WorkerThreadFunc 的函数才能使其工作。它必须匹配 System.Threading.ThreadStart代表签名。

private void WorkerThreadFunc()
{
}

如果您不在此函数中添加任何内容,线程将启动然后立即关闭,因此您必须在其中添加一些逻辑,以便在您工作时基本上保持线程处于事件状态。这就是 _shutdownEvent 派上用场的地方。

private void WorkerThreadFunc()
{
    while (!_shutdownEvent.WaitOne(0)) {
        // Replace the Sleep() call with the work you need to do
        Thread.Sleep(1000);
    }
}

while 循环检查 ManualResetEvent 以查看它是否已“设置”。由于我们在上面使用 false 初始化了对象,因此此检查返回 false。在循环内,我们休眠 1 秒。您需要将其替换为您需要执行的工作 - 监控代理设置等。

最后,在 Windows 服务的 OnStop() 回调中,您希望向线程发出停止运行的信号。这很容易使用 _shutdownEvent

protected override void OnStop()
{
     _shutdownEvent.Set();
     if (!_thread.Join(3000)) { // give the thread 3 seconds to stop
         _thread.Abort();
     }
} 

希望这对您有所帮助。

关于c# - Windows服务持续运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4864673/

相关文章:

windows - 没有网络访问权限的虚拟服务帐户,如 NT AUTHORITY\LocalService

c# - 更改数据集时,SqlDependency 不会触发 OnChange 事件

windows-services - window服务每天自动重启现有的tomcat服务

c# - 如何使用 amazon sdk 为虚名域生成预签名的 Amazon S3 url?

c# - 如何使用 C# API 定义-fun

c# - 没有值的键值对容器

.net - Windows 安装程序 : Error 1001, CustomAction _xxxxx.install 返回实际错误代码 1603

c# - 包装异步方法

c# - 运行时 ASP.NET 确认对话框

c# - 一个程序集中的多个服务。安装程序如何知道要安装和启动哪个服务?