【问题标题】:Event Handler Not Firing From Foreach Loop事件处理程序未从 Foreach 循环触发
【发布时间】:2023-03-06 10:09:01
【问题描述】:
        private string _orderUrl;

        IEnumerable<NotificationMessage> notificationMessages = JsonConvert.DeserializeObject<IEnumerable<NotificationMessage>>(response);

        foreach (var notificationMessage in notificationMessages)
        {
            bool success = notificationMessage.Success;
            string type = notificationMessage.Type;
            string message = notificationMessage.Message;
            _orderUrl = notificationMessage.OrderUrl;

            if (success)
            {
                _notifyIcon.BalloonTipTitle = BalloonTitle;
                _notifyIcon.BalloonTipText = type + @": " + message;
                _notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
                _notifyIcon.BalloonTipClicked += BalloonTip_Click;
                _notifyIcon.ShowBalloonTip(10000);

                SystemSounds.Exclamation.Play();

                Thread.Sleep(10000);
            }
        }
    }

    void BalloonTip_Click(object sender, EventArgs e)
    {
        string urlBase = ConfigurationManager.AppSettings["UrlBase"];
        string target = urlBase + _orderUrl;
        System.Diagnostics.Process.Start(target);
    }

循环每 10 秒显示一次气球通知。这个想法是点击气球来访问浏览器的 URL。

我有两个问题。我可以从循环外部为每个气球触发 Click 事件吗? _orderUrl 参数是否会传递给事件处理程序?

现在,Click 事件没有触发。

【问题讨论】:

  • 您应该使用计时器而不是 Thread.Sleep。您当前正在锁定 UI 线程,这就是 click 事件未触发的原因(它在 UI 线程上运行)。
  • 您是否尝试过使用两个不同的线程?那个睡眠电话看起来很可疑。

标签: c# winforms events


【解决方案1】:

永远不要在 UI 线程上使用 Thread.Sleep,它会锁定并且您的应用会变得无响应。

// This needs to be a class variable (not local)
private Queue<NotificationMessage> notificationMessages = new Queue<NotificationMessage>();


void LoadMessages()
{
    private string _orderUrl;
    this.notificationMessages = JsonConvert.DeserializeObject<IEnumerable<NotificationMessage>>(response);

   ShowNextMessage();
}

void ShowNextMessage()
{
    if (notificationMessages.Count == 0) return;
    var notificationMessage = notificationMessages.Dequeue();

    bool success = notificationMessage.Success;
    string type = notificationMessage.Type;
    string message = notificationMessage.Message;
    _orderUrl = notificationMessage.OrderUrl;

    if (success)
    {
        _notifyIcon.BalloonTipTitle = BalloonTitle;
        _notifyIcon.BalloonTipText = type + @": " + message;
        _notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
        _notifyIcon.BalloonTipClicked += BalloonTip_Click;
        _notifyIcon.ShowBalloonTip(10000);

        SystemSounds.Exclamation.Play();
    }
}

void BalloonTip_Click(object sender, EventArgs e)
{
    string urlBase = ConfigurationManager.AppSettings["UrlBase"];
    string target = urlBase + _orderUrl;
    System.Diagnostics.Process.Start(target);

    ShowNextMessage();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-15
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多