Consider I have an event listener in C# that waits for the data from the serial port, which are returned once captured by setting some status. In the main loop I look into that status and based on that I decide what to do next.
To do that I first write the command into the serial port and wait for the output by using Thread.Sleep method. By the time when the main thread is still in sleep mode, the event listener would already send the data and there is nothing done to handle that scenario.
Can anyone please tell me what would be the outcome in this condition and how to prevent it?
The code would be like
bool status = false;
//main thread
private void Main()
{
//some Code
serialPort.Write("something");
//Wait for 10 sec
Thread.Sleep(10000);
while(status == false)
{
// do something else
}
}
//Ignore the syntax, this is just to make people understand
public void OndataReceived(object sender, EventArg arg)
{
//Function that recieve the data
Receive();
//change status to true
status = true;
return;
}