-1

I am thinking of using an LED like indicator light to display (green to red) when something is completed.

My current code goes something like this:

bool saveSingleSampleDone = false;

private void btnSingleSampleData_Click(object sender, EventArgs e)
{
    LEDindicator.Image = global::OnlineData.Properties.Resources.green_indicator;
    LEDindicator.Refresh();

    while (!saveSingleSampleDone);

    if (saveSingleSampleDone)
    {
        LEDindicator.Image = global::OnlineData.Properties.Resources.red_indicator;
        LEDindicator.Refresh();
    }
}

where saveSingleSampleDone flag is triggered from a thread running.

The issue is that the code will hang due to the while (!saveSingleSampleDone); statement, and that this way of writing is not neat anyway.

I was thinking of starting a timer in a thread to track the start and the end of the event, but my question is that is there a more elegant way of achieving the indication without using a timer?

John Tan
  • 1,331
  • 1
  • 19
  • 35
  • So you want to flash the LED: a timer is the answer. Start it in your click event handler, and revert it when the timer expires. Remember to work out what happens if there is another click event before the timer expires (either restarting the timer or allowing the existing one to continue can make sense, but you need decide). – Richard Jan 18 '15 at 08:53

1 Answers1

0

What you should use is the BackgroundWorker

jaywayco
  • 5,846
  • 6
  • 25
  • 40