I am building a GUI that reads in serial data into a textbox. If there is a problem and the instrument stops working, I want the textbox to turn red. I am trying to do this with a timer, so the timer will read data every 30 seconds, then if there is no data, the timer will then turn the textbox red. However, nothing happens when I unplug the instrument. How can I turn the textbox red when the instrument stops working?
Here is the relevant code:
private void timer1_Tick(object sender, EventArgs e)
{
DateTime timenow = DateTime.Now;
TimeSpan span = GPSlastdatatime - timenow;
timespan = Convert.ToDouble(span.TotalSeconds);
if (timespan > 30)
{
TextBox1.BackColor = Color.Red;
}
}
I assume "GPSlastdatatime" is set whenever data comes in?
As is, your "timespan" value is going to be negative.
Switch the order of your subtraction line:
By the way, you could do it all in one line like this: