multithreading - Safe Multi-thread access in C# -


i trying implement chat. have taken code here

when send messages have error:

control 'listmessage' name accessed thread other thread created on.

where listmessage listview added visual studio.

       private void buttonsend_click(object sender, eventargs e)         {             //convert string message byte[]             asciiencoding aencoding = new asciiencoding();             byte[] sendingmessage = new byte[1500];             sendingmessage = aencoding.getbytes(textmessage.text);             //sending encoded message             sck.send(sendingmessage);             //adding list box             listmessage.items.add("me:" + textmessage.text);             textmessage.text = "";         }          private void messagecallback(iasyncresult aresult)         {             try             {                 byte[] receiveddata = new byte[1500];                 receiveddata = (byte[])aresult.asyncstate;                 //converting byte[] string                 asciiencoding aencoding = new asciiencoding();                 string receivedmessage = aencoding.getstring(receiveddata);                  //adding message list box ///// error given in following line/////                 listmessage.items.add("friend:" + receivedmessage);                  buffer = new byte[1500];                 sck.beginreceivefrom(buffer, 0, buffer.length, socketflags.none, ref epremote, new asynccallback(messagecallback), buffer);             }             catch (exception ex)             {                 messagebox.show(ex.tostring());             }         } 

i have tried use invoke described here have errors

        private void messagecallback(iasyncresult aresult)         {             try             {                 byte[] receiveddata = new byte[1500];                 receiveddata = (byte[])aresult.asyncstate;                 //converting byte[] string                 asciiencoding aencoding = new asciiencoding();                 string receivedmessage = aencoding.getstring(receiveddata);                  //adding message list box                 //listmessage.items.add("friend:" + receivedmessage);                 listmessage.additemsafe("friend:" + receivedmessage);                  buffer = new byte[1500];                 sck.beginreceivefrom(buffer, 0, buffer.length, socketflags.none, ref epremote, new asynccallback(messagecallback), buffer);             }             catch (exception ex)             {                 messagebox.show(ex.tostring());             }         }          private void additemsafe(string text)         {             if (this.invokerequired)             {                 additemsafecallback = new additemsafecallback(additemsafe);                 this.invoke(a, new object[] { text });             }             else             {                 this.items.add(text);             }         }          delegate void additemsafecallback(string text); 

erros are:

  • 'listbox' not contain definition fo 'additemsafe' , no extension methode. 'additemsafe' accepting first argument of type 'listbox'could found (are missing using directive or assembly reference'
  • 'myusercontrol' not contain definition fo 'additemsafe' , no extension methode. 'item' accepting first argument of type 'listbox'could found (are missing using directive or assembly reference'

my questions:

  • do believe way try fix initial problem 1 ?
  • if yes, problem solution try implement

yes, approach quite correct, did miss few things implementation.

the additemsafe method method inside form descendant class, not method on listmessage (whatever type may be). when call that, have specify method name , parameter , not call on object instance.

... asciiencoding aencoding = new asciiencoding(); string receivedmessage = aencoding.getstring(receiveddata); //adding message list box //listmessage.items.add("friend:" + receivedmessage);  // !!!!note difference here: additemsafe("friend:" + receivedmessage); ... 

then, inside additemsafe method, in single threaded environment, i.e. add items of listmessage. if invoke required, wrap whole thing delegate.

private void additemsafe(string text) {   if (this.invokerequired)   {     additemsafecallback = new additemsafecallback(additemsafe);     this.invoke(a, new object[] { text });   }   else   {     // !!!!note difference here:     listmessage.items.add(text);   } } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -