如何在窗口服务中每30秒创建一次自动通知

时间:2016-07-13 05:15:04

标签: c# windows

    protected override void OnStart(string[] args)
    {
        try
        {
            TraceService("start service");
            timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
            timer.Interval = 30000;
            timer.Enabled = true;
        }
        catch (Exception ex)
        {

        }
    }

    protected override void OnStop()
    {
        try
        {
            timer.Enabled = false;
            TraceService("stopping service");
        }
        catch (Exception ex)
        {

        }
    }
    public void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        try
        {
            var notification = new System.Windows.Forms.NotifyIcon()
            {
                Visible = true,
                Text = "Test Notify",
                BalloonTipTitle = "test title notify",
                BalloonTipText = "Testing"                    
            };

            notification.ShowBalloonTip(10000);
            //System.Threading.Thread.Sleep(10000);
            //notification.Dispose();
            TraceService("Another entry at " + DateTime.Now);
        }
        catch (Exception ex)
        {
            TraceService("StackTrace : " + ex.StackTrace);
            TraceService("Message : " + ex.Message);
        }
    }


}

}'

每30秒自动通知

2 个答案:

答案 0 :(得分:1)

Windows服务本身无法显示桌面迭代。它无法显示窗口。

答案 1 :(得分:0)

您可以在Windows窗体应用程序中使用Timer来实现此目的 - https://msdn.microsoft.com/en-us/library/system.windows.forms.timer(v=vs.110).aspx

相关问题