Question

I need to implement something. Something that could do some certain task in my program. For example every ten seconds, write something into a log in a file. Of course it suppose to run in a background thread.

Where should I dig? I am not so familiar with multithreading. I've heard about BackgroundWorker class, but I'm not sure if it is appropriate here..

Was it helpful?

Solution

Use System.Threading.Timer, it will run a task in a ThreadPoool thread. That is the most efficient way for this.

Here is an example, every 10 seconds:

Timer aTimer = new System.Threading.Timer(MyTask, null, 0, 10000);

static void MyTask(object state)
{
  ...
}

OTHER TIPS

Actually for WPF DispatcherTimer would be much better than the Async timer.

You could use the backgroundworker class for this, but it sounds like you just need to use Timer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top