C# 从另一个线程发送winforms中的弹出通知

C# 从另一个线程发送winforms中的弹出通知,c#,windows,multithreading,winforms,popup,C#,Windows,Multithreading,Winforms,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 fro

我在我的基本聊天程序中遇到了一个独特的小错误,该错误表示我无法从其他线程发送邮件:

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();
        }
    }
但是,通知弹出窗口库没有调用所需的方法,因此我在该修复上运气不佳


有人能帮忙吗?

这些方法与复制的有点不同,所以我想在将它们标记为复制之前,我把它们放在这里

以下是解决此问题的方法:

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;
        }
    }

这些方法与复制的有点不同,所以我想在将它们标记为复制之前,我把它们放在这里

以下是解决此问题的方法:

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;
        }
    }

“通知弹出窗口库”是什么?我不记得Winform有这样的东西。是这个nuget软件包:应该有帮助。谢谢@Sinatr!这似乎奏效了!“通知弹出窗口库”是什么?我不记得Winform有这样的东西。是这个nuget软件包:应该有帮助。谢谢@Sinatr!这似乎奏效了!