I am working on a program where I need to activate a timer from a different thread. I've shortened the code to this example below.
static class Program
{
public static System.Windows.Forms.Timer TimerVideo = new System.Windows.Forms.Timer();
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
TimerVideo.Interval = 1000;
TimerVideo.Tick += new EventHandler(TimerVideo_Tick);
new Thread(() =>
{
test RunTimer = new test();
RunTimer.StartTimer();
}).Start();
Application.Run();
}
private static void TimerVideo_Tick(object sender, EventArgs e)
{
Console.WriteLine("Running");
}
}
class test
{
public void StartTimer()
{
Program.TimerVideo.Start();
}
}
Even if I have to code the timer in the class that would be fine with me, but I did try that and it didn't work either, namely the timer did not fire and nothing was printed out to the console.