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 ();

return true;
}
Catch
{
return false;
}
}
Private void Timer_Elapsed (object sender, ElapsedEventArgs e)
{
String [] STR=SerialPort. GetPortNames ();
If (flagRetry==false)
{
FlagRetry=true;

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

CodePudding user response:

reference young programmer 1/f, small don's reply:
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!!!


You should know the System. The Timers. The Timer is itself multithreaded Timer? Didn't see your code, the estimate is made in the timer method you close port operation, and then in the other thread is using the port,

CodePudding user response:

reference ying1234 reply: 3/f
Quote: refer to 1st floor young programmers don's reply:

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!!!


You should know the System. The Timers. The Timer is itself multithreaded Timer? Didn't see your code, the estimate is made in the timer method you close port operation, and then in the other thread is using the port,


Now know is multithreaded, I did close port is in the timer and then open another serial port, is ConfigComPort (STR) [pIdx++] this method is to open the port, and then went back to the main thread to execute a program, this is I like to think of a problem, think back to the main thread of the port should be open state,
Don't know if it because I am a breakpoint debugging state, so the trigger abnormal rates were particularly high
But I let the program no breakpoint operation is also very easy to broke when switching a serial port,
Then add a Thread to the main Thread code. Sleep (150); And the if (! SP. Isopen) {break; }, prevent abnormal again, will I have this problem, but also is one of the stable than before,

CodePudding user response:

At the same time Application. DoEvents (); Also executed asynchronously,

CodePudding user response:

Timer timer best before implementation will perform set to false to complete set to true, after a similar lock operation

CodePudding user response:

Especially in the case of time interval is less than the execution time,

CodePudding user response:

So, do not use single step debugging,

You have to habits, it is not a simple console, do you want to step through? If you override the WndProc, don't say you have a timer, you are a blank form without any control, you try the effect of single step,

What your exception to fly everywhere, no relationship,
That's because you closed the serial port, didn't turn off the timer
  •  Tags:  
  • C#