public static bool canStop;
public bool CanStop {get { return canStop; } }
try{
foreach (Control btn in flpButtons.Controls)
{
canStop = false;
if (btn is JoystickSample.Button)
{
((JoystickSample.Button)btn).ButtonStatus =
jst.Buttons[((JoystickSample.Button)btn).ButtonId - 1];
}
}
}finally {canStop = true;}
Edit to add button from comments:
private void buttonStop_Click(object sender, EventArgs e)
{
if(CanStop == true)
{
if (SerialPort.IsOpen)
{
freeze();
SerialPort.Close();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
timerSending.Stop();
}
}
else
{
MessageBox.Show("Not finished");
}
}
This is pretty rough, But you want to do something like this and then check if "CanStop" is true or false when you click Stop.
Basically, You're stopping before the Foreach loop finishes.
Also, Your application is probably hanging because you aren't invoking when you update your UI.