Hi i have stepped into some problem related to timer. hope somebody can help..
- I have a windows form containing a button
- when i click on that button i start a parameterised thread
Thread thread1 = new Thread(new ParameterizedThreadStart( execute2));
thread1.Start(externalFileParams);
- the code inside the thread executes very well
- at the last line of this thread i start a timer
.
public void execute2(Object ob)
{
if (ob is ExternalFileParams)
{
if (boolean_variable== true)
executeMyMethod();//this also executes very well if condition is true
else
{
timer1.enabled = true;
timer1.start();
}
}
}
}
5 but the tick event of the timer is not fired
I am working on VS2008 3.5 framework. I have dragged the timer from toolbox and set its Interval
to 300 also tried to set Enabled
true/false
method is timer1_Tick(Object sender , EventArgs e)
but its not fired
can anybody suggest what I am doing wrong?
You could try to start the timer this way:
Add in form constructor this:
Add this method to Form1:
On button click event add this:
This timer is already threaded so no need to start a new thread.
It is true what Matías Fidemraizer says. But, there is a work around...
When you have a control on your form that is invokable (eg. a statusbar), just invoke that one!
C# Code sample:
I would use a BackgroundWorker (instead of a raw thread). The main thread would subscribe to the worker's RunWorkerCompleted event: The event fires in your main thread when the thread completes. Use this event handler to restart your timer.
System.Windows.Forms.Timer works in a single-threaded application.
Check this link:
Remarks says:
Read more "Remarks" section and you'll find that Microsoft recommends that you use this timer synchronizing it with the UI thread.