Question is about using a timer in each created thread.
I’m writing an application which collects a CPU data from a PC every 30 seconds. It works if I collect data only from 1 PC and using 1 timer, without any thread. Now, I want to collect data from 2 PCs simultaneously. To this end, I decided to use threading where each thread will work for each PC and will have its own timer. So, 2 threads with 2 timers for 2 PCs. I use System.Windows.Threading.DispatcherTimer to create a timer by each thread. But, the issue is that the created timer doesn't start working (i.e. doesn't call timerTick). Hence, if I create a timer without threads, then it works correctly, whereas a thread created timer doesn't work. :(
Maybe the considered solution is not correct and need some changes. Please, help me to understand the problem.
Here is a simple version of the code:
void CreateThread()
{
Thread First = new Thread(new ThreadStart(FirstThreadWork));
First.Start();
}
private void FirstThreadWork()
{
System.Windows.Threading.DispatcherTimer timer;
timer = new System.Windows.Threading.DispatcherTimer();
timer.Tick += new EventHandler(timerTick);
timer.Interval = new TimeSpan(0, 0, 30);
timer.Start();
}
private void timerTick(object sender, EventArgs e)
{
MessageBox.Show("Show some data");
}