BuddyJoe
BuddyJoe

Reputation: 71111

Background Process (periodic "service") in WinForm Application

What is the best practice for setting up a background process that runs every 10 minutes in a WinForm project? Is it to use a backgroundworker off of the form? Or is there a more generic way that would apply to many more project styles?

Maybe some code I should call right before the line:

 Application.Run(new Form1());

Upvotes: 0

Views: 1383

Answers (2)

Reed Copsey
Reed Copsey

Reputation: 564433

Typically, if you want a Windows Forms application to run some code on a regular interval, using a Windows.Forms.Timer on one of your forms is an appropriate way to handle the notifications.

However, as you seem to have realized, this will require you to have a form up and running, and tie you to the Windows Forms infrastructure.

Another, alternative, approach would be to use a System.Threading.Timer class, which notifies you on a background thread. However, if you use this approach, you'll need to use some form of synchronization if you want your "process" to interact with the user interface. The best platform neutral approach (works with Windows Forms + WPF) would be to use SyncrhonizationContext to marshal back to the UI thread, if required.

Upvotes: 2

Giorgi
Giorgi

Reputation: 30883

You can use Task Scheduler to schedule an application.

If you want to have a timer in your application you can either use a timer or use a library such as Quartz.NET

Upvotes: 1

Related Questions