Home > Net > Stuck, solving!!!!!! Timer midway through ran out of the events to the thread of execution is why
Stuck, solving!!!!!! Timer midway through ran out of the events to the thread of execution is why
Time:11-26
I set up a timer for a serial port communication timeout retransmission mechanism, : I met in the debugger trigger retransmission mechanism automatically jump to the next set in the serial code, switch after a serial port (close the current serial ports and then open the next serial port), thread start an error, prompt the port is closed, According to my personal understanding, not should after a timeout event execution back to the thread continue to execute the code, so it should be under close a serial port and I open a serial port, change again after the state steps should just returned to the thread that executes the while loop,
//timer initialization Private void TimerInit () { The timer=new System. The Timers. The timer (); The timer. The Interval=500; The timer. Elapsed +=Timer_Elapsed; The timer. The AutoReset=true; The timer. Enabled=true; } //close a serial port Private void CloseComPort SerialPort (sP) { If (sP) IsOpen==true) { Application.doevents (); SP. The Close ();//here after execution of the thread is an error in port was closed, } } Private bool ConfigComPort (string pName) { Try { SP. PortName=pName; SP. BaudRate=9600; SP. DataBits=8; SP., Parity=System. IO. Ports. Parity. None; SP., StopBits=System. IO. Ports. StopBits. One; SP. WriteBufferSize=1024; SP. ReadBufferSize=1024; SP. DtrEnable=true; SP. RtsEnable=true; SP. The Open ();
If ((- timesForRetry)==0)//resend five times { If ((state==state. STATE_Read_HandShake) & amp; & (FLG==1)) { CloseComPort (sP);//resend after closing a serial port to search for the next five times a serial port If (pIdx & lt; STR. Length) { If (ConfigComPort (STR [pIdx++]))//serial port initialization, open the serial port { TimesForRetry=5; FlagRetry=false; State=state. STATE_Send_HandShake; //return; } } The else {flagTransmitting=false; } } The else { FlagTransmitting=false; } } The else { If ((state==state. STATE_Read_HandShake) & amp; & (FLG==1)) { State=state. STATE_Send_HandShake; } FlagRetry=false; } }
CodePudding user response:
Set breakpoints, step through feeling this timer events like thread is interspersed with handover execution, step in their event is also performed on, the next step is to execute the code to the thread, and then returned to the next timer event, each great god trouble to give directions, this program is very important to me!!!
CodePudding user response:
Add a lock in the timer and thread is the most direct solution, you can modify the logic to add a status identification, in when receiving data to determine the state, not enter serial switch