从另一个线程以winforms形式发送通知弹出窗口 [英] Sending a notification popup in winforms from another thread

查看:63
本文介绍了从另一个线程以winforms形式发送通知弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在基本聊天程序中遇到一个独特的小错误,该错误指出我无法发送 Notification Popup :

I've run into a unique little error in my basic chat program which states that I cannot send a Notification Popup from another thread:

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();时会发生这种情况:

This occurs when I call popupNotification.Popup(); from this method:

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 + "\"");
    }

我正在尝试创建看起来像这样的跨线程代码:

I'm trying to create the cross-thread code which should look like this:

public delegate void UpdatePopup(PopupNotifier notificationPopup);

void sendAPopup(PopupNotifier notificationPopup)
    {
        if (notificationPopup.InvokeRequired)
        {
            Invoke(new UpdatePopup(sendAPopup), new object[] { notificationPopup });
        }
        else
        {
            notificationPopup.Popup();
        }
    }

但是,通知弹出窗口库没有Invoke Required方法,因此对于该修复程序我不走运.

However, the notification popup window library doesn't have a Invoke Required method, so I'm out of luck on that fix.

任何人都可以帮忙吗?

推荐答案

方法与重复项略有不同,因此我认为在将它们标记为重复项之前将它们放在此处.

The methods are a bit different than the duplicate so I figured I put them here before marking them as a duplicate.

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

Here's how this issue is fixed:

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

这篇关于从另一个线程以winforms形式发送通知弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆