public void timer_thing()
{
Thread timer = new Thread(new ThreadStart(() =>
{
Thread.Sleep(500);
if (is_mouse_down)
timer1.Enabled = true;
}
));
timer.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
//some stuff happens here
}
As you can see I want the thread to activate the timer. But the timer doesn’t activate. I guess this is not the right way. Or i’m missing something.
It could be that
is_mouse_downisfalsewhenever the thread hits that instruction. The thread is not going to magically wait for it to turntrue.However, you have another, bigger problem to worry about. The thing is you cannot touch any UI element from a worker thread or any other other than the UI thread. This includes the
System.Windows.Forms.Timer. All sorts of undefined chaos may ensue. Your application may fail unpredictably and spectacularly.It is not really clear to me why a thread is needed in the first place. Can you not handle the
Control.MouseDownevent and enable the timer in the event handler for that event? That is how I would solve the problem.