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事件未触发。
答案 0 :(得分:2)
不要在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();
}