multithreading - how do i convert these c# winform codes to be compatible on c# wpf? -
hi im working on project uses invoke , threads.. simple remote desktop program chat.. got sample here on internet in c# winform, convert wpf.. have no problem in sending message client using wpf program cannot receive ( or cannot read) sent messages others.. think has thread , invoke method, read wpf invoke differently , did try dispatcher.invoke, still doesnt trick
pls hellp
here's code
wait = new thread(new threadstart(waitfordata)); wait.start(); that snippet above executed when successful connection made in tcpclient
private void waitfordata() { try { networkstream read = tcpclnt.getstream(); while (read.canread) { byte[] buffer = new byte[64]; read.read(buffer, 0, buffer.length); s = new asciiencoding().getstring(buffer); system.console.writeline("recieved data:" + new asciiencoding().getstring(buffer)); rcvmsg = new asciiencoding().getstring(buffer) + "\n"; hasnewdata = true; bool f = false; f = rcvmsg.contains("##"); bool comand = false; comand = rcvmsg.contains("*+*-"); /*file receive*/ if (f) { string d = "##"; rcvmsg = rcvmsg.trimstart(d.tochararray()); int lastlt = rcvmsg.lastindexof("|"); rcvmsg = rcvmsg.substring(0, lastlt); networkstream ns = tcpclnt.getstream(); if (ns.canwrite) { string datas = "^^y"; byte[] bf = new asciiencoding().getbytes(datas); ns.write(bf, 0, bf.length); ns.flush(); } try { new recieve_file().recieve_file(rcvmsg); } catch (exception ec) { system.console.writeline(ec.message); } } /*command-shutdown/restart/logoff*/ else if (comand) { string com = "*+*-"; rcvmsg = rcvmsg.trimstart(com.tochararray()); execute_command(rcvmsg); } else { this.invoke(new setoutput(setout)); thread.sleep(1000); } } } catch (exception ex) { wait.abort(); output.text += "error..... " + ex.stacktrace; } } the snippet above code listens if there message or command.. line this.invoke(new setoutput(setout)) code appending text in rtb
hope me thanks
you've posted lot of code, i'm assuming it's call control.invoke causing problem. in wpf, use dispatcher.invoke (or dispatcher.begininvoke) instead, via dispatcher property on relevant ui element.
i'd encourage to:
- refactor code smaller methods
- stop catching
exceptionexcept @ top level of large operation (it should fall-back; catch specific exceptions) - start following .net naming conventions
- add
usingdirectivesystemcan writeconsole.writelineinstead ofsystem.console.writelineeverywhere - use
encoding.asciiinstead of creating newasciiencodingeach time need one - use
streamreaderread character data stream, instead of reading binary data first , encoding it - for either
streamortextreader, don't ignore return valueread- tells how many bytes or characters have been read
Comments
Post a Comment