Tulpep PopupNotifier 不适用于计时器 [英] Tulpep PopupNotifier not working with Timer
问题描述
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);
}
}
}
这里我使用 Tulpep 通知来创建桌面通知.我的表单中有一个开始按钮.单击开始按钮时,计时器开始弹出桌面通知.但它仅在我不对 MessageBox.Show("");
发表评论时才显示通知.如果我删除或评论 MessageBox.Show("");
它不显示通知.我在这两种情况下都进行了调试,两种情况都没有错误或异常.
Here i am using Tulpep notification for create desktop notification. I have one start button in my form. When start button clicked, timer start to pop desktop notification. But it is shows notification only when i not comment on MessageBox.Show("");
. and if i remove or comment MessageBox.Show("");
it is not showing notification. I debug in both case, there is no error or exception in both case.
有人知道为什么会这样吗?
Is any one have idea why is this happening?
我使用的是 .net framework 4.5.2,visual studio 2015,windows 8
.
推荐答案
PopupNotifier
需要在 UI-Thread 之外调用.由于您的计时器的处理程序在不同的线程中运行,您需要调用您的表单来解决问题.
PopupNotifier
needs to be called out of the UI-Thread. Since the handler of your timer runs in a different thread you need to invoke your form to solve the problem.
this.Invoke((MethodInvoker)delegate
{
PopupNotifier pop = new PopupNotifier();
pop.TitleText = "Test";
pop.ContentText = "Hello World";
pop.Popup();
});
这篇关于Tulpep PopupNotifier 不适用于计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!