c# - Sending a notification popup in winforms from another thread -


i've run unique little error in basic chat program states cannot send notification popup thread:

an exception of type 'system.invalidoperationexception' occurred in system.windows.forms.dll not handled in user code  additional information: cross-thread operation not valid: control '' accessed thread other thread created on. 

this occurs when call popupnotification.popup(); method:

void chatserver_ondatareceived(object sender, receivedarguments e)     {         string machine = e.name;         string message = e.receiveddata;         popupnotification.titletext = "new  message";         popupnotification.contenttext = machine + " sent message @ " + datetime.now.toshorttimestring() +             ", saying  \"" + message + "\"";         popupnotification.popup();         changetextboxcontents(e.name + " sent message @ " + datetime.now.toshorttimestring() +             ", saying  \"" + e.receiveddata + "\"");     } 

i'm trying create cross-thread code should this:

public delegate void updatepopup(popupnotifier notificationpopup);  void sendapopup(popupnotifier notificationpopup)     {         if (notificationpopup.invokerequired)         {             invoke(new updatepopup(sendapopup), new object[] { notificationpopup });         }         else         {             notificationpopup.popup();         }     } 

however, notification popup window library doesn't have invoke required method, i'm out of luck on fix.

can help?

the methods bit different duplicate figured put them here before marking them duplicate.

here's how issue fixed:

void chatserver_ondatareceived(object sender, receivedarguments e)     {         string machine = e.name;         string message = e.receiveddata;         popupnotification.titletext = "new  message";         popupnotification.contenttext = machine + " sent message @ " + datetime.now.toshorttimestring() +             ", saying  \"" + message + "\"";         popupmethod(); //call method works cross-thread         changetextboxcontents(e.name + " sent message @ " + datetime.now.toshorttimestring() +             ", saying  \"" + e.receiveddata + "\"");     } ///this method works cross thread checking if invoke required ///and if so, popup shown delegate across thread void popupmethod()     {         if(invokerequired)         {             this.invoke(new methodinvoker(delegate {                 popupnotification.popup();             }));             return;         }     } 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

android - Pass an Serializable object in AIDL -