Blu*_*lue 2 c# notifications timer winforms
using System;
using System.Data.SQLite;
using System.Drawing;
using System.Timers;
using System.Windows.Forms;
using Tulpep.NotificationWindow;
public partial class Form1 : Form
{
System.Timers.Timer timer = null;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
if (timer == null)
{
timer = new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler(ObjTimer_Elapsed);
timer.Interval = 10000;
timer.Start();
}
}
private void ObjTimer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
PopupNotifier pop = new PopupNotifier();
pop.TitleText = "Test";
pop.ContentText = "Hello World";
pop.Popup();
//MessageBox.Show(""); !!! here is problem !!!
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我使用Tulpep通知创建桌面通知.我的表单中有一个开始按钮.单击开始按钮时,计时器开始弹出桌面通知.但只有当我不评论时它才会显示通知 MessageBox.Show("");.如果我删除或评论MessageBox.Show("");它没有显示通知.我调试两种情况,两种情况都没有错误或异常.
有人知道为什么会这样吗?
我在用.net framework 4.5.2,visual studio 2015, windows 8.
PopupNotifier需要从UI-Thread中调用.由于计时器的处理程序在不同的线程中运行,因此您需要调用表单来解决问题.
this.Invoke((MethodInvoker)delegate
{
PopupNotifier pop = new PopupNotifier();
pop.TitleText = "Test";
pop.ContentText = "Hello World";
pop.Popup();
});
Run Code Online (Sandbox Code Playgroud)