Windows服务不断运行 [英] Windows Service to run constantly

查看:246
本文介绍了Windows服务不断运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

香港专业教育学院创建了一个名为 ProxyMonitor Windows服务,目前在IM这里的服务是安装在舞台和卸载就是我想要的方式。

Ive created a windows Service called ProxyMonitor and im currently at the stage where the service is installs and uninstall's the way I want it.

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

So I execute the application like so:

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

pretty自我解释,然后我到 SERVICES.MSC 键,并启动该服务,但是当我这样做,我得到了以下信息:

Pretty self explanatory, and then I got to services.msc and and start the service, but when I do this I get the following message:

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

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

我的code看起来像这样:

My code looks like so:

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

然后ProxyMonitor类中我有:

And then within ProxyMonitor class I have:

public ProxyMonitor()
{
}

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

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

的onStop()我只是改变了运行变量;

and onStop() I just change the running variable to false;

我需要做的,使服务不断活跃,因为我将需要监控网络,我需要跟踪的变化等。

What would I need to do to make the Service constantly active, as I would need to be monitoring the network I need to trace changes etc.

更新:1

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

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

ThreadWorker ProxyEventLogger.WriteEntry(主线程进入)不被解雇。

推荐答案

的OnStart()回调需要及时归还,所以你想踢关在那里所有的工作将执行一个线程。我建议加入以下字段类:

The OnStart() callback needs to return in a timely fashion, so you'll want to kick off a thread where all your work will be performed. I would recommend adding the following fields to your class:

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

_Thread 字段将举行一个参考 System.Threading.Thread 对象,你在<$创建C $ C>的OnStart()回调。在 _shutdownEvent 字段包含一个系统级的事件结构将被用来通知线程停止服务停止运行。

The _thread field will hold a reference to the System.Threading.Thread object you create in the OnStart() callback. The _shutdownEvent field holds a system-level event construct that will be used to signal the thread to stop running on service shutdown.

的OnStart()的回调,创建和启动线程。

In the OnStart() callback, create and start your thread.

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

您需要一个以名为 WorkerThreadFunc 函数这个工作。它相匹配的<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.threadstart.aspx\"><$c$c>System.Threading.ThreadStart委托签名。

You need a function named WorkerThreadFunc in order for this to work. It has to match the System.Threading.ThreadStart delegate signature.

private void WorkerThreadFunc()
{
}

如果你不把任何东西在这个函数中,该线程将启动,然后立即关闭,所以你必须摆在那里一些逻辑,而你做你的工作,基本上保持线程活着。这就是来自 _shutdownEvent 派上用场了。

If you don't put anything in this function, the thread will start up and then immediately shutdown, so you have to put some logic in there that basically keeps the thread alive while you do your work. This is where the _shutdownEvent comes in handy.

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

while循环检查的ManualResetEvent ,看它是否被套或没有。由于我们初始化物体上述虚假,此检查返回false。在循环内部,我们睡了1秒。你会想,你需要做的工作,以取代本 - 显示器代理设置等。

The while loop checks the ManualResetEvent to see if it is "set" or not. Since we initialized the object with false above, this check returns false. Inside the loop, we sleep for 1 second. You'll want to replace this with the work you need to do - monitor proxy settings, etc.

最后,在您的Windows服务的调用OnStop()的回调,要信号线程停止运行。这是很容易使用 _shutdownEvent

Finally, in the OnStop() callback of your Windows Service, you want to signal the thread to stop running. This is easy using the _shutdownEvent.

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

希望这有助于。

这篇关于Windows服务不断运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆