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-Thread 中调用。由于计时器的处理程序在不同的线程中运行,因此您需要调用表单来解决问题。

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/

10-11 22:32
查看更多