如何在窗口服务中为每 30 秒创建一次自动通知
本文关键字:一次 通知 创建 窗口 服务 | 更新日期: 2023-09-27 17:56:36
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 秒自动通知一次
Windows 服务本身不能显示桌面的迭代。它不能显示窗口。
您可以在 Windows 窗体应用程序中使用计时器来实现此目的 - https://msdn.microsoft.com/en-us/library/system.windows.forms.timer(v=vs.110).aspx