c# - New line disappears after Invoke -
i've got silly minor problem here. context, using c#, i'm communicating between sockets , want display output worker threads.
i have these lines display output:
txtoutput.appendtext("client - sending following message: " + encoding.utf8.getstring(bytes) + environment.newline); txtoutput.invoke(new action(() => txtoutput.appendtext("client - server returned message: " + str + environment.newline)));
the first line added program's main thread, second worker thread.
the first line shows fine, second 1 too. when sequence again, first line b pasted onto second line a, second line b looking correct again. second line's newline disappearing, don't understand why or how fix this.
can try this. think problem encoding.
when send message:
string message = input(textbox ex); utf8encoding utf8 = new utf8encoding(); byte[] encodedbytes = utf8.getbytes(message);
and keep code @ receive.
i tested following:
public partial class form1 : form { private thread thread; private byte[] encodedbytes2; private byte[] encodedbytes; public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { utf8encoding utf8 = new utf8encoding(); string unicodestring = "quick brown fox"; encodedbytes = utf8.getbytes(unicodestring); encodedbytes2 = new byte[20]; encodedbytes2[0] = (byte)'r'; encodedbytes2[1] = (byte)'e'; for(int = 0; < 5; i++) textbox1.appendtext("encoded " + encoding.ascii.getstring(encodedbytes) + environment.newline); for(int = 0; < 5; i++) textbox1.appendtext(" not encoded " + encoding.ascii.getstring(encodedbytes2) + environment.newline); thread = new thread(threadwork); thread.isbackground = true; thread.start(); } private void threadwork() { (int = 0; < 5; i++) textbox1.invoke(new action(() => textbox1.appendtext("thread encoded " + encoding.ascii.getstring(encodedbytes) + environment.newline))); (int = 0; < 5; i++) textbox1.invoke(new action(() => textbox1.appendtext("thread not encoded " + encoding.ascii.getstring(encodedbytes2) + environment.newline))); } }
and output turned out in multiline textbox:
encoded quick brown fox
encoded quick brown fox
encoded quick brown fox
encoded quick brown fox
encoded quick brown fox
not encoded re not encoded re not encoded re not encoded re not encoded rethread encoded quick brown fox thread encoded quick brown fox
thread encoded quick brown fox
thread encoded quick brown fox
thread encoded quick brown fox
thread not encoded rethread not encoded rethread not encoded rethread not encoded rethread not encoded re
Comments
Post a Comment