Performing operation in a timely manner
What is the best architecture to perform custom operations (application-level) at an interval?
Define a static class, define a timer in it, and set the interval. Perform the operation in timer's tick event: Seems like a bit dirty and unreliable.
Define a windows service, handle operations in the service: Reliable, but needs much more work and it's preferable to have a simpler solution with fewer lines of code. Also, this need开发者_StackOverflow中文版s access to the host system (Not applicable if you're on a shared hosting).
Is there any specific design pattern which is considered best practice for this kind of work?
Create a Console Application and use Windows Task Scheduler.
One of the serious problem with (1) solution is that if PC is restarted, switchedoff/switchedon after a couple of days is that you need restart your app.
void InstallMeOnStartUp()
{
try
{
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Assembly curAssembly = Assembly.GetExecutingAssembly();
key.SetValue(curAssembly.GetName().Name, curAssembly.Location);
}
catch (Exception ex)
{
//
}
}
Install on startup run. Using this combination can add kind of more "reliability" to your app + easier debugging then service + easier setup then service == easier maintanance then service.
Regards.
精彩评论