从我的服务重新启动另一个Windows服务的问题 [英] Problem to restart another windows service from my service

查看:97
本文介绍了从我的服务重新启动另一个Windows服务的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在主题中所说,由于Windows 10中的打印问题,我有运行64位服务。所以现在需要在我的服务启动时实现从我的服务重新启动anoter服务。但这不起作用。它只是输入方法service.start()并且什么也不做......



如果我从单个窗口表单应用程序尝试一切都很好。



我的尝试:



//我有一个方法

Task.Run(()=> RegisteSomeService())

//。

//。

//。

//当此方法进入异常时我想尝试使用此代码重新启动服务...

//首先我询问服务是否停止,如果是,则仅在然后没有重新启动... //但是没有什么异常不知道为什么

As i said in subject i have service who is running 64bit because of the print problems in windows 10. So now need to implement while my service is started to restart anoter service from my service. But that doesn't work. It just enter the method service.start() and do nothing...

If i try that from single windows form application everything is fine.

What I have tried:

//i have a method
Task.Run(() => RegisteSomeService())
//.
//.
//.
//when this method go in exception i wanna try to restart service with this code...
//first i ask if service is stoped if yes then only start if doesnt then restart ... //but there is nothing in exception have no idea why

public static void RestartService(string serviceName)
        {
            try
            {
                var timeout = TimeSpan.FromMilliseconds(60 * 1000);

                $"GetStatus([{serviceName}]) - Start".Trace();

                if (GetStatus(serviceName) != ServiceControllerStatus.Stopped)
                {
                    try
                    {
                        var service = new ServiceController(serviceName);

                        service.Stop();
                        service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                else
                {
                    try
                    {
                        $"Service Start - Start".Trace();
                        var serviceStart = new ServiceController(serviceName);
                        serviceStart.Start();
                        serviceStart.WaitForStatus(ServiceControllerStatus.Running, timeout);
                        $"Service Start - End".Trace();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static ServiceControllerStatus GetStatus(string serviceName)
        {

            ServiceController service = null;

            try
            {
                service = ServiceController.GetServices().Where(s => s.ServiceName == serviceName).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw new Exception("Service controller status - GetStatus: " + ex.ToString());
            }

            $"GetStatus([{serviceName}]) - END {service.Status}".Trace();
            return service.Status;
        }

推荐答案

GetStatus([{serviceName}]) - 开始.Trace();

if(GetStatus(serviceName)!= ServiceControllerStatus.Stopped)
{
try
{
var service = new ServiceController(serviceName);

service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped,timeout);

service.Start();
service.WaitForStatus(ServiceControllerStatus.Running,timeout);
}
catch(exception ex)
{
throw ex;
}
}
其他
{
尝试
{
"GetStatus([{serviceName}]) - Start".Trace(); if (GetStatus(serviceName) != ServiceControllerStatus.Stopped) { try { var service = new ServiceController(serviceName); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch (Exception ex) { throw ex; } } else { try {


服务开始 - 开始.Trace( );
var serviceStart = new ServiceController(serviceName);
serviceStart.Start();
serviceStart.WaitForStatus(ServiceControllerStatus.Running,timeout);
"Service Start - Start".Trace(); var serviceStart = new ServiceController(serviceName); serviceStart.Start(); serviceStart.WaitForStatus(ServiceControllerStatus.Running, timeout);


Service Start - End.Trace();
}
catch(exception ex)
{
throw ex;
}
}
}
catch(Exception ex)
{
throw ex;
}
}

public static ServiceControllerStatus GetStatus(string serviceName)
{

ServiceController service = null;

尝试
{
service = ServiceController.GetServices()。其中​​(s => s.ServiceName == serviceName).FirstOrDefault();
}
catch(exception ex)
{
抛出新的异常(服务控制器状态 - GetStatus:+ ex.ToString());
}
"Service Start - End".Trace(); } catch (Exception ex) { throw ex; } } } catch (Exception ex) { throw ex; } } public static ServiceControllerStatus GetStatus(string serviceName) { ServiceController service = null; try { service = ServiceController.GetServices().Where(s => s.ServiceName == serviceName).FirstOrDefault(); } catch (Exception ex) { throw new Exception("Service controller status - GetStatus: " + ex.ToString()); }


这篇关于从我的服务重新启动另一个Windows服务的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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