Background Process (periodic "service") in WinForm Application
What is the best practice for setting up a background process tha开发者_运维知识库t 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());
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.
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
精彩评论