1 year ago
#328826
Gomunkwan
How can I Form timer stop from other thread?
I'm using Form timer fired by serialport datareceived. Sometimes timer should stopped by user but timer doesn't stop but it doesn't
Here is my summarized code.
private delegate void MessageDelegate(string str);
private MessageDelegate addmsg;
private void SerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (!StopRequests_DataReceivedRunning) //initially false and set true by StopMenuItem_Click()
{
addmsg = new MessageDelegate(InterpretMessage);
//...get seriaport string...
Invoke(addmsg, new object[] { strDataReceived.ToString() });
}
}
private void InterpretMessage(string ReceivedMessage)
{
//...do something
if(condition is met)
{
timer.Interval = 10000
timer.Start();
}
}
private void timer_Tick(object sender, EventArgs e)
{
Console.WriteLine(timer.Enabled.ToString()); ;
//<do-work>
}
private void StopMenuItem_Click(object sender, EventArgs e)
{
timer.Enabled = false;
StopRequests_DataReceivedRunning = true;
}
If I click StopMenuItem during running of timer, it doesn't stop and finally raise timer_Tick event(console output is true). Also, it looks timer restart at the moment, timer.Enabled = false.
Edit: I added more codes.
c#
multithreading
winforms
timer
restart
0 Answers
Your Answer