Bad Threading..

G

Guest

Got the below class, it has one thread that does a continuous loop getting
network packets, it all seems to work well, except when i try to open a
results Form "myTestForm" from within the thread, it just hangs.. tried
threading TestForm, but not quite sure how to do that and whether or not it
would solve my problem..




public class ListenerForm : System.Windows.Forms.Form{

private void Form_Load(object sender, System.EventArgs e){
System.Threading.Thread t = new System.Threading.Thread(new
System.Threading.ThreadStart(ListenLoop));
t.Start();
}

private void ListenLoop(){
while(listening){
TestForm myTestForm = new TestForm();
myTestForm.Visible = true;
}
}
}
 
G

Guest

Found a workaround, using Invoke.. though seems like a strange solution to
the problem..

private void ListenLoop(){
while(listening){
this.Invoke(new EventHandler(new EventHandler(test)));
}
}

public void test(object sender, EventArgs e){
TestForm myTestForm = new TestForm();
myTestForm.Visible = true;
}
 
T

Truong Hong Thi

though seems like a strange solution to
the problem..
It is not strange. It is the standard way to invoke a control's method
from other thread. Make sure you always do this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top