views:

190

answers:

2

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());
+1  A: 

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

Giorgi
+1 on the Quartz.NET. Maybe a little overkill for what I need right now. But awesome to know this is out there.
tyndall
+2  A: 

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.

Reed Copsey
Actually for the stuff I'm doing I think I won't be coming back to the UI from these jobs. If that is the case then is System.Threading.Timer the better approach? +1 on the SyncrhonizationContext. Have some more reading to do!
tyndall
If it's not UI related, then use a threading.timer - it works in a separate thread, so it's a bit nicer if you don't need a UI.
Reed Copsey