0
Reply

Thread problem

bram 0

bram 0

May 7 2004 12:44 PM
1.5k
Hello, I want a separate thread to handle serial communication in a simple way: Forever do the following: - Send message - Wait for reply - Act on it I'm using events for the reply because a while(!received) type solution is very slow when there are a lot of threads. This is basically what I'm doing: class Handler { Thread thread = new Thread(new ThreadStart(work)); Reply reply; public Handler() { commObject.Reply += new EvenReply(receiveReply); thread.start(); } public void work() { while(true) { sendCommand(); thread.Suspend(); // wait for reply handleReply(); } } public void receiveReply(Reply r) { this.reply = r; thread.Resume(); } } Unfortunately, this causes problems. Sometimes the event is already raised before the thread is stopped and I can't use a critical sections because this will prevent the thread from ever being resumed. Any ideas on how to do this?!