如何在 c#.net 中以编程方式重新启动我的窗口服务

Posted

技术标签:

【中文标题】如何在 c#.net 中以编程方式重新启动我的窗口服务【英文标题】:how restart my window service programmatically in c#.net 【发布时间】:2011-02-26 14:47:53 【问题描述】:

任何人都可以帮助我...我如何在 c#.net 中每 15 分钟以编程方式重新启动我的窗口服务,请帮助我.. 我已经在我的代码中完成了diz方式 我一直喜欢这种方式,直到在课堂页面[RunInstaller(true)]

public class ProjectInstaller : System.Configuration.Install.Installer 
private System.ServiceProcess.ServiceProcessInstaller 
                                  serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1; 
public ProjectInstaller()
   InitializeComponent();
 
private void InitializeComponent() 
 
   this.serviceProcessInstaller1 = 
     new System.ServiceProcess.ServiceProcessInstaller(); 
   this.serviceInstaller1 = 
     new System.ServiceProcess.ServiceInstaller(); 
   this.serviceProcessInstaller1.Account = 
     System.ServiceProcess.ServiceAccount.LocalSystem; 
   this.serviceProcessInstaller1.Password = null;
   this.serviceProcessInstaller1.Username = null; 
   this.serviceInstaller1.ServiceName = "MyNewService"; 
   this.serviceInstaller1.StartType = 
     System.ServiceProcess.ServiceStartMode.Automatic;

    this.Installers.AddRange
     (new System.Configuration.Install.Installer[] 
    
       this.serviceInstaller1, 
       this.serviceInstaller1);
   

【问题讨论】:

为什么需要每 15 分钟重新启动一次?如果您希望代码每 15 分钟运行一次,请使用计时器并将计时器 Tick 事件设置为应该运行的代码。 每 15 分钟重新启动一次服务听起来像是在尝试解决其他问题。究竟为什么要重新启动服务?服务被设计为长时间连续运行,如果这不是您想要的,服务可能是该工作的错误工具。 顺便问一下,您的 Windows 服务与您标记问题的 asp.net 标记有什么关系?如果与问题无关,则应删除该标签。 @albin ...谢谢你的回答是的!在一个特定的根文件夹中,一些文件会来来去去,我的服务将很好地开始工作,一段时间后,工作现在再次完成,一些文件将在那个特定的时间到达那个根文件夹,我通过手动重启来完成,这样我在问 【参考方案1】:

您可以使用此代码重新启动服务:

using System.Diagnostics;

public static void RestartService(string serviceName)

var psi = new ProcessStartInfo("net.exe", "stop " + serviceName);
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = true;
psi.WorkingDirectory = Environment.SystemDirectory;
var st = Process.Start(psi);
st.WaitForExit();

psi = new ProcessStartInfo("net.exe", "start " + serviceName);
psi.UseShellExecute = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.WorkingDirectory = Environment.SystemDirectory;
st = Process.Start(psi);
st.WaitForExit();

我得到了这个代码here,对其进行了测试,它工作正常。使用间隔为 15 分钟的计时器每 15 分钟调用一次。

【讨论】:

【参考方案2】:

有几种方法可以做到这一点:

    将您的服务更改为在 AppDomain 中托管当前进程的轻量级存根。使用计时器服务卸载并重新启动您的 AppDomain。

    创建两个服务。将一个服务设为计时器,并使用ServiceController 以编程方式重新启动此服务,以访问您的服务以停止并重新启动它。

【讨论】:

【参考方案3】:

您不应重新启动整个服务。 相反,您可以在服务中创建一个定期触发的计时器

这样

private void StartTimer()

    System.Threading.Timer timer = 
        new System.Threading.Timer(
            TimerCompleted, 
            null, 
            TimeSpan.FromMinutes(15), 
            TimeSpan.FromMinutes(15));


private void TimerCompleted(object state)

    // Call your action here
    ProcessFiles();

从您的服务开始调用StartTimer,并将您的所有工作放在ProcessFiles

但是,如果您实际上只是想监视目录中的更改,则可以改用 FileSystemWatcher。它可以在文件系统发生变化时立即通知您。

【讨论】:

突然投反对票是怎么回事?在 SO 上,在投票时写评论以指出可能的改进是一种很好的方式。【参考方案4】:

使用以下命令创建批处理文件:

net stop "服务名称" 网络启动“服务名称”

使用 windows 调度程序并创建一个每 15 分钟运行一次此脚本的调度。

【讨论】:

以上是关于如何在 c#.net 中以编程方式重新启动我的窗口服务的主要内容,如果未能解决你的问题,请参考以下文章

如何在iOS中以编程方式重新启动iPhone应用程序

如何在三星设备中以编程方式启用自动启动选项?

如何在 .NET 中以编程方式启动 Amazon EC2 实例

如何在 Android 中以编程方式触发自定义信息窗口

在 Spring Boot 应用程序中以编程方式重新启动 HikariPool?

如何在 MI 4i 安全应用程序中以编程方式为我的应用程序启用自动启动选项?