从另一个线程在 winforms 中发送通知弹出窗口
Sending a notification popup in winforms from another thread
我 运行 在我的基本聊天程序中遇到了一个独特的小错误,该错误表明我无法从另一个线程发送 Notification Popup:
An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code
Additional information: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
当我从这个方法调用 popupNotification.Popup();
时会发生这种情况:
void ChatServer_OnDataReceived(object sender, ReceivedArguments e)
{
string machine = e.Name;
string message = e.ReceivedData;
popupNotification.TitleText = "New message";
popupNotification.ContentText = machine + " sent a message at " + DateTime.Now.ToShortTimeString() +
", saying \"" + message + "\"";
popupNotification.Popup();
changeTextBoxContents(e.Name + " sent a message at " + DateTime.Now.ToShortTimeString() +
", saying \"" + e.ReceivedData + "\"");
}
我正在尝试创建应该如下所示的跨线程代码:
public delegate void UpdatePopup(PopupNotifier notificationPopup);
void sendAPopup(PopupNotifier notificationPopup)
{
if (notificationPopup.InvokeRequired)
{
Invoke(new UpdatePopup(sendAPopup), new object[] { notificationPopup });
}
else
{
notificationPopup.Popup();
}
}
但是,通知弹出窗口 window 库没有 Invoke Required 方法,所以我没能解决这个问题。
有人能帮忙吗?
这些方法与重复的有点不同,所以我想我在将它们标记为重复之前将它们放在这里。
解决此问题的方法如下:
void ChatServer_OnDataReceived(object sender, ReceivedArguments e)
{
string machine = e.Name;
string message = e.ReceivedData;
popupNotification.TitleText = "New message";
popupNotification.ContentText = machine + " sent a message at " + DateTime.Now.ToShortTimeString() +
", saying \"" + message + "\"";
popupMethod(); //call the method that works cross-thread
changeTextBoxContents(e.Name + " sent a message at " + DateTime.Now.ToShortTimeString() +
", saying \"" + e.ReceivedData + "\"");
}
///This method works cross thread by checking if an invoke is required
///and if so, then the popup is shown with a delegate across the thread
void popupMethod()
{
if(InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate {
popupNotification.Popup();
}));
return;
}
}
我 运行 在我的基本聊天程序中遇到了一个独特的小错误,该错误表明我无法从另一个线程发送 Notification Popup:
An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code
Additional information: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
当我从这个方法调用 popupNotification.Popup();
时会发生这种情况:
void ChatServer_OnDataReceived(object sender, ReceivedArguments e)
{
string machine = e.Name;
string message = e.ReceivedData;
popupNotification.TitleText = "New message";
popupNotification.ContentText = machine + " sent a message at " + DateTime.Now.ToShortTimeString() +
", saying \"" + message + "\"";
popupNotification.Popup();
changeTextBoxContents(e.Name + " sent a message at " + DateTime.Now.ToShortTimeString() +
", saying \"" + e.ReceivedData + "\"");
}
我正在尝试创建应该如下所示的跨线程代码:
public delegate void UpdatePopup(PopupNotifier notificationPopup);
void sendAPopup(PopupNotifier notificationPopup)
{
if (notificationPopup.InvokeRequired)
{
Invoke(new UpdatePopup(sendAPopup), new object[] { notificationPopup });
}
else
{
notificationPopup.Popup();
}
}
但是,通知弹出窗口 window 库没有 Invoke Required 方法,所以我没能解决这个问题。
有人能帮忙吗?
这些方法与重复的有点不同,所以我想我在将它们标记为重复之前将它们放在这里。
解决此问题的方法如下:
void ChatServer_OnDataReceived(object sender, ReceivedArguments e)
{
string machine = e.Name;
string message = e.ReceivedData;
popupNotification.TitleText = "New message";
popupNotification.ContentText = machine + " sent a message at " + DateTime.Now.ToShortTimeString() +
", saying \"" + message + "\"";
popupMethod(); //call the method that works cross-thread
changeTextBoxContents(e.Name + " sent a message at " + DateTime.Now.ToShortTimeString() +
", saying \"" + e.ReceivedData + "\"");
}
///This method works cross thread by checking if an invoke is required
///and if so, then the popup is shown with a delegate across the thread
void popupMethod()
{
if(InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate {
popupNotification.Popup();
}));
return;
}
}