如何在不弹出用户的情况下停止窗口服务 [英] How to stop a window service without user pop-up

查看:63
本文介绍了如何在不弹出用户的情况下停止窗口服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了Windows服务.在OnStart()中,我首先检查许可证.如果许可证不存在,该服务将停止.我尝试了几种不同的方法来做到这一点,例如引发异常.有用.唯一的问题是,它弹出一个对话框,提示本地计算机上的服务先启动然后停止.xxxxx",并且用户需要单击确定"以退出消息对话框.

I created a windows service. In OnStart(), I check license first. If the license is not there, the service stops. I have tried several different ways to do that, such as throw an exception. It works. The only problem is that it pops up a dialog saying "service on local computer started then stopped. xxxxx" and user need to click OK to discharge the message dialogue.

Is there a way to stop the service w/o any front-end pop-ups?

推荐答案

您需要在启动服务时添加一个延迟,然后才能停止该服务.希望您在另一个类中拥有大多数服务功能,并且ServiceBase只是一个外壳,在这种情况下,您可以像下面这样.

You need to add a delay before stopping the service when it is starting. Hopefully you have most of the service functionality in another class and the ServiceBase is just a shell in which case you can have something like below.

protected override void OnStart(string[] args)
{
    if(licenceIsValid)
    {
        _realService.Start();
    }
    else
    {
        ThreadPool.QueueUserWorkItem(delegate
            {
                Thread.Sleep(TimeSpan.FromSeconds(10));
                Stop();
            });
    }
}


这篇关于如何在不弹出用户的情况下停止窗口服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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