c# - Tulpep PopupNotifier 无法与计时器一起使用

标签 c# winforms timer notifications

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 notification用于创建桌面通知。我的表单中有一个开始按钮。单击开始按钮时,计时器开始弹出桌面通知。但它仅在我不评论 MessageBox.Show(""); 时显示通知。如果我删除或评论 MessageBox.Show(""); 它不会显示通知。我在这两种情况下都进行了调试,两种情况下都没有错误或异常。

有人知道为什么会这样吗?

我正在使用 .net framework 4.5.2,visual studio 2015, windows 8

最佳答案

PopupNotifier 需要从 UI 线程中调用。由于您的计时器的处理程序在不同的线程中运行,您需要调用您的表单来解决问题。

this.Invoke((MethodInvoker)delegate
{
    PopupNotifier pop = new PopupNotifier();
    pop.TitleText = "Test";
    pop.ContentText = "Hello World";
    pop.Popup();
});

关于c# - Tulpep PopupNotifier 无法与计时器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45955220/

相关文章:

c# - 为什么这个赋值不是线程安全的?

c# - 更新绑定(bind)到它的 ObservableCollection 后,DataGrid 不会更新

c# - Visual 2010 中的 C# Windows 窗体中是否有转发器控件?还是另一种选择?

javascript - 以 1 秒的间隔重复某个函数一定次数

ios - 在 iOS 中每秒更新一次 TableView 中单元格中标签的文本

c# - 如何在 List<T> 中的 foreach 中编写变量循环级别

c# - 使用类来保存与软件消息相关的字符串

C# Treeview 检查节点是否存在

c# - 从radgridview中的组合框获取显示值

excel - windows定时器VBA改变速度