c# - Why doesn't this thread ever end? -
i'm trying control piece of test equipment , need sequencing correct of how communicate it.
first call startgettingtracedata()
. time in future call stopgettingtracedata()
attempt end gettracedata()
function instead of re-launching itself. never happens. in fact, never line donetraces.set()
on line bool timedout = !donetraces.waitone(10000)
, timedout
true;
private static autoresetevent donetraces = new autoresetevent(false); private void gettracedata() { byte[] receivedbytes = new byte[1]; if (connection.readdata(receivedbytes) && receivedbytes[0] == 192) processincomingtrace(); thread.sleep(100); if (runtracequeryworker) new thread(gettracedata).start(); else { thread.sleep(200); donetraces.set(); } } private void startgettingtracedata() { runtracequeryworker = true; new thread(gettracedata).start(); } private bool stopgettingtracedata() { runtracequeryworker = false; bool timedout = !donetraces.waitone(10000); return timedout; }
any thoughts on going on?
edit:
here connection.readdata(...) function. it's serial connection way.
public bool readdata(byte[] responsebytes) { int bytesexpected = responsebytes.length, offset = 0, bytesread; while (bytesexpected > 0 && (bytesread = myserialport.read(responsebytes, offset, bytesexpected)) > 0) { offset += bytesread; bytesexpected -= bytesread; } return bytesexpected == 0; }
rather recusively calling gettracedata again, should use while loop looking @ condition this:
private static autoresetevent donetraces = new autoresetevent(false); private void gettracedata() { { byte[] receivedbytes = new byte[1]; if (connection.readdata(receivedbytes) && receivedbytes[0] == 192) processincomingtrace(); thread.sleep(100); } while (runtracequeryworker) thread.sleep(200); donetraces.set(); } private void startgettingtracedata() { runtracequeryworker = true; new thread(gettracedata).start(); } private bool stopgettingtracedata() { runtracequeryworker = false; bool timedout = !donetraces.waitone(10000); return timedout; }
it's impossible know why code freezing without understanding readdata
& processincomingtrace()
do.
Comments
Post a Comment