Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何安装/运行windows服务_.net_Windows_C# 4.0_Service - Fatal编程技术网

.net 如何安装/运行windows服务

.net 如何安装/运行windows服务,.net,windows,c#-4.0,service,.net,Windows,C# 4.0,Service,我在Visual C#2010中编译了一个服务。如果单击BService.exe,它将启动,但也会显示一个消息框:“无法从命令行或调试器启动服务。必须安装Windows服务(使用installutil.exe),然后使用ServerExplorer、Windows服务管理工具或NET start命令启动。”如果单击“确定”,程序将关闭。如果我使用C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\installutil.exe安装If,并从services

我在Visual C#2010中编译了一个服务。如果单击BService.exe,它将启动,但也会显示一个消息框:“无法从命令行或调试器启动服务。必须安装Windows服务(使用installutil.exe),然后使用ServerExplorer、Windows服务管理工具或NET start命令启动。”如果单击“确定”,程序将关闭。如果我使用C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\installutil.exe安装If,并从services.msc启动,它也会启动,但不会显示MessageBox.show(“sdas”),因此不起作用。如何安装/运行该服务

public partial class BService : ServiceBase
{
    private System.Timers.Timer timer;
    public BService()
    {
        timer = new System.Timers.Timer(2000);
        timer.Enabled = true;
        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        InitializeComponent();
    }
    public void Start() { timer.Start();}
    public void Stop() { timer.Stop();}
    protected override void OnStart(string[] args) { this.Start();}
    protected override void OnStop() { this.Stop();}

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        MessageBox.Show("sdas");
    }
}

使用以下代码使用C#安装windows服务:

使用以下代码使用C运行win Server#


Windows服务(从Vista开始)不再允许向用户显示UI,没有窗体、消息框、控制台窗口、没有.Net Framework更新对话。。。没有什么你无法回避这个问题,如果可以的话,微软会修补你的方法,这样它就不会工作太久。这是报纸上的章节

更改代码,使其记录事件并改为查看事件查看器(请参见)。

“不显示消息框,因此不起作用”——这是错误的。服务不应该与桌面交互。某个地方有一个消息框,你就是看不到它。
InitializeComponent()
?为什么使用表单应用程序作为服务?
public void InstallWinService(string winServicePath)
{
    try
    {
        ManagedInstallerClass.InstallHelper(new string[] { winServicePath});
    }
    catch (Exception)
    {

        throw;
    }
}
public void StartService(string serviceName)
{
    ServiceController service = new ServiceController(serviceName);
    try
    {
        service.MachineName = "localhost";
        service.Start();
        service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
    }
    catch (Exception ex)
    {
        throw ex;
    }
}