Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
C# 仅当使用messagebox.show时,windows服务才起作用如何避免_C#_.net_Windows Services - Fatal编程技术网

C# 仅当使用messagebox.show时,windows服务才起作用如何避免

C# 仅当使用messagebox.show时,windows服务才起作用如何避免,c#,.net,windows-services,C#,.net,Windows Services,这是我的密码 protected override void OnStart(string[] args) { timAutoUpdate.Enabled = true; MessageBox.Show("started"); } protected override void OnStop() { timAutoUpdate.Enabled = false; System.Windows.Forms.MessageBox.Show("Service stopp

这是我的密码

protected override void OnStart(string[] args)
{
    timAutoUpdate.Enabled = true;
    MessageBox.Show("started");
}

protected override void OnStop()
{
    timAutoUpdate.Enabled = false;
    System.Windows.Forms.MessageBox.Show("Service stopped");
}

private void timAutoUpdate_Tick(object sender, EventArgs e)
{
    MessageBox.Show("Ticked");
}
它在启动事件后显示消息被勾选。如果我在Onstart事件中删除messagebox,则勾选事件不起作用。它不显示任何消息。请指定背后的原因。我只是在勾选事件中保留了简单的消息框,而不是我的代码。请告诉我实现它的方法

 protected override void OnStart(string[] args)
{
    timAutoUpdate.Enabled = true;
}

protected override void OnStop()
{
    timAutoUpdate.Enabled = false;
}

private void timAutoUpdate_Tick(object sender, EventArgs e)
{
    MessageBox.Show("Ticked");
}

你用的是什么定时器?是System.Windows.Forms.Timer吗

如果是这种情况,您需要更改它,因为此计时器不适用于Windows服务

请尝试以下代码:

    protected override void OnStart(string[] args)
    {
        scheduleTimer = new System.Threading.Timer(new TimerCallback(AutoTick), null, 0, 3000);
        eventLogEmail.WriteEntry("Test");
    }
    private void AutoTick(object state)
    {
        MessageBox.Show("Ticked")
    }

从Vista升级服务无法显示用户界面。不要试图从服务中显示UI。如果我没有显示UI勾号事件未触发:您可能错误地使用了UI计时器。使用适用于服务的计时器。请使用或,而不是System.Windows.Forms.timer。相关问题:。强制性评论:您可能不应该首先创建服务。在83.7%的情况下,不显示窗口的标准应用程序是一个更好、更简单的选择。目前,我已将我的System.windows.Forms.Timer更改为System.Timers.Timer。在调用MessageBox之前,您做得很好。不要在服务中这样做!