【发布时间】: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 线程上运行)。
-
您是否尝试过使用两个不同的线程?那个睡眠电话看起来很可疑。