multithreading - C# Changing controls from another thread, invoke not working -
this code:
public partial class form1 : form { void threadproc(string data) { int i; console.writeline("received: {0}", data); this.invoke((methodinvoker)delegate() { string[] tok = data.split('|'); int rowindex = 10000 * 10000; foreach (datagridviewrow row in usersbets.rows) { if ((row.cells[1].value != null) && (row.cells[1].value.tostring().equals(tok[0]))) { if (row.index < rowindex) rowindex = row.index; } } if (rowindex != 10000 * 10000) { datagridviewrow row = usersbets.rows.sharedrow(rowindex); string user = row.cells[0].value.tostring(); treenode[] tnarr = treeview1.nodes.find(tok[0], false); if (tnarr[0] != null) { this.usersbets.rows.insert(rowindex + 1, null, tok[0], null, tok[1], tok[2], null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, system.datetime.now); if (this.usersbets.rows[rowindex].cells[0].value.equals("-")) { this.usersbets.rows[rowindex + 1].visible = true; } else { this.usersbets.rows[rowindex + 1].visible = false; } this.usersbets.rows[rowindex + 1].cells[1].style.forecolor = color.white; tnarr[0].nodes.insert(0, tok[1]); } } else { rowindex = 0; this.usersbets.rows.insert(rowindex, "+", tok[0], null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); this.usersbets.rows.insert(rowindex + 1, null, tok[0], null, tok[1], tok[2], null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, system.datetime.now); if (this.usersbets.rows[rowindex].cells[0].value.equals("-")) { this.usersbets.rows[rowindex + 1].visible = true; } else { this.usersbets.rows[rowindex + 1].visible = false; } this.usersbets.rows[rowindex + 1].cells[1].style.forecolor = color.white; } int max = 0; (i = 3; < tok.length - 1; i++) { if (convert.toint32(tok[i]) == 0) { this.usersbets.rows[rowindex + 1].cells[i + 2].style.backcolor = system.drawing.color.green; } else if ((convert.toint32(tok[i]) > 10) && (convert.toint32(tok[i]) < 20)) { this.usersbets.rows[rowindex + 1].cells[i + 2].style.backcolor = system.drawing.color.blue; } else if (convert.toint32(tok[i]) == 20) { this.usersbets.rows[rowindex + 1].cells[i + 2].style.backcolor = system.drawing.color.yellow; } else if ((convert.toint32(tok[i]) > 30) && (convert.toint32(tok[i]) < 40)) { this.usersbets.rows[rowindex + 1].cells[i + 2].style.backcolor = system.drawing.color.red; } } switch (max) { case 0: this.usersbets.rows[rowindex + 1].cells[2].style.backcolor = system.drawing.color.limegreen; break; case 1: this.usersbets.rows[rowindex + 1].cells[2].style.backcolor = system.drawing.color.yellow; break; case 2: this.usersbets.rows[rowindex + 1].cells[2].style.backcolor = system.drawing.color.red; break; case 3: this.usersbets.rows[rowindex + 1].cells[2].style.backcolor = system.drawing.color.blue; break; } tok = tok[tok.length - 1].split(';'); max = convert.toint32(tok[0]); (i = 0; < tok.length; i++) { if (convert.toint32(tok[i]) == 0) { this.usersbets.rows[rowindex + 1].cells[i + 2].style.backcolor = system.drawing.color.green; } else if ((convert.toint32(tok[i]) > 10) && (convert.toint32(tok[i]) < 20)) { this.usersbets.rows[rowindex + 1].cells[i + 2].style.backcolor = system.drawing.color.blue; } else if (convert.toint32(tok[i]) == 20) { this.usersbets.rows[rowindex + 1].cells[i + 2].style.backcolor = system.drawing.color.yellow; } else if ((convert.toint32(tok[i]) > 30) && (convert.toint32(tok[i]) < 40)) { this.usersbets.rows[rowindex + 1].cells[i + 2].style.backcolor = system.drawing.color.red; } } switch (max) { case 0: this.usersbets.rows[rowindex].cells[2].style.backcolor = system.drawing.color.limegreen; break; case 1: this.usersbets.rows[rowindex].cells[2].style.backcolor = system.drawing.color.yellow; break; case 2: this.usersbets.rows[rowindex].cells[2].style.backcolor = system.drawing.color.red; break; case 3: this.usersbets.rows[rowindex].cells[2].style.backcolor = system.drawing.color.blue; break; } }); } public form1() { initializecomponent(); } private void button1_click_1(object sender, eventargs e) { // set tcplistener on port 12000. int32 port = 12000; ipaddress localaddr = ipaddress.parse("110.169.128.289"); console.write("waiting connection... "); string data = null; // perform blocking call accept requests. // user server.acceptsocket() here. tcpclient client = new tcpclient("192.168.118.145", port); try { //buffer reading data byte[] bytes = new byte[256]; console.writeline("connected!"); // stream object reading , writing networkstream stream = client.getstream(); streamreader reader = new streamreader(stream); // enter listening loop. while (true) { int i; // loop receive data sent client. if ((data = reader.readline()) != null) { thread thread = new thread(() => threadproc(data)); thread.start(); } } } catch (socketexception err) { console.writeline("socketexception: {0}", err); } { // stop listening new clients. client.close(); } } }
for reason code in this.invoke((methodinvoker)delegate() scope isen't happening. sitting google days now... can tell me why , how solve it?
thanks lot
Comments
Post a Comment