C# 事件处理程序未从Foreach循环触发 private string\u orderUrl; IEnumerable notificationMessages=JsonConvert.DeserializeObject(响应); foreach(notificationMessages中的var notificationMessage) { bool success=notificationMessage.success; 字符串类型=notificationMessage.type; 字符串消息=notificationMessage.message; _orderUrl=notificationMessage.orderUrl; 如果(成功) { _notifyIcon.BalloodTiptTitle=BalloodTitle; _notifyIcon.BalloodTiptText=type+@:“+消息; _notifyIcon.balloottipicon=ToolTipIcon.Info; _notifyIcon.balloottipclicked+=balloottip\u单击; _notifyIcon.showBallootTip(10000); SystemSounds.感叹号.Play(); 睡眠(10000); } } } 无效气球提示\u单击(对象发送者,事件参数e) { 字符串urlBase=ConfigurationManager.AppSettings[“urlBase”]; string target=urlBase+\u orderUrl; 系统。诊断。过程。启动(目标); }
循环每10秒显示一个气泡通知。这样做的目的是单击气球以访问浏览器URL 我有两个问题。我可以从循环外部为每个气球触发Click事件吗?是否将_orderUrl参数传递给事件处理程序C# 事件处理程序未从Foreach循环触发 private string\u orderUrl; IEnumerable notificationMessages=JsonConvert.DeserializeObject(响应); foreach(notificationMessages中的var notificationMessage) { bool success=notificationMessage.success; 字符串类型=notificationMessage.type; 字符串消息=notificationMessage.message; _orderUrl=notificationMessage.orderUrl; 如果(成功) { _notifyIcon.BalloodTiptTitle=BalloodTitle; _notifyIcon.BalloodTiptText=type+@:“+消息; _notifyIcon.balloottipicon=ToolTipIcon.Info; _notifyIcon.balloottipclicked+=balloottip\u单击; _notifyIcon.showBallootTip(10000); SystemSounds.感叹号.Play(); 睡眠(10000); } } } 无效气球提示\u单击(对象发送者,事件参数e) { 字符串urlBase=ConfigurationManager.AppSettings[“urlBase”]; string target=urlBase+\u orderUrl; 系统。诊断。过程。启动(目标); },c#,winforms,events,C#,Winforms,Events,循环每10秒显示一个气泡通知。这样做的目的是单击气球以访问浏览器URL 我有两个问题。我可以从循环外部为每个气球触发Click事件吗?是否将_orderUrl参数传递给事件处理程序 现在,单击事件没有触发。永远不要使用线程。在UI线程上睡眠,它将锁定,您的应用程序将变得无响应 private string _orderUrl; IEnumerable<NotificationMessage> notificationMessages = JsonCo
现在,单击事件没有触发。永远不要使用线程。在UI线程上睡眠,它将锁定,您的应用程序将变得无响应
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);
}
//这需要是一个类变量(不是局部变量)
private Queue notificationMessages=新建队列();
void LoadMessages()
{
私有字符串_orderUrl;
this.notificationMessages=JsonConvert.DeserializeObject(响应);
ShowNextMessage();
}
void ShowNextMessage()
{
if(notificationMessages.Count==0)返回;
var notificationMessage=notificationMessages.Dequeue();
bool success=notificationMessage.success;
字符串类型=notificationMessage.type;
字符串消息=notificationMessage.message;
_orderUrl=notificationMessage.orderUrl;
如果(成功)
{
_notifyIcon.BalloodTiptTitle=BalloodTitle;
_notifyIcon.BalloodTiptText=type+@:“+消息;
_notifyIcon.balloottipicon=ToolTipIcon.Info;
_notifyIcon.balloottipclicked+=balloottip\u单击;
_notifyIcon.showBallootTip(10000);
SystemSounds.感叹号.Play();
}
}
无效气球提示\u单击(对象发送者,事件参数e)
{
字符串urlBase=ConfigurationManager.AppSettings[“urlBase”];
string target=urlBase+\u orderUrl;
系统。诊断。过程。启动(目标);
ShowNextMessage();
}
您应该使用计时器而不是Thread.Sleep。您当前正在锁定UI线程,这就是为什么没有触发click事件(它在UI线程上运行)的原因。您是否尝试过使用两个不同的线程?那个睡眠电话看起来可疑。
// 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();
}