I`d like some job be periodically done. This job might be writing files, some processing with resourse handling + sometimes it might be time-consuming. A good candidate for periodic tasks is a
Timer
clas from
System.Timers
namespace. However, it raises
Elapsed
event each time in different thread.
Now imagine I have 500ms
Timer
period, and the thread from previous
Elapsed
event did not finished, so the resourses were not released. A thread from "current"
Elapsed
event tries to get theses resourses, and we`ve got a conflict. I see a solition like the following:
class FolderMonitor
{
Timer monitoringTimer = new Timer(500);
public FolderMonitor(string folderName)
{
monitoringTimer.Elapsed += new ElapsedEventHandler(monitoringTimer_Elapsed);
monitoringTimer.Start();
}
void monitoringTimer_Elapsed(object sender, ElapsedEventArgs e)
{
monitoringTimer.Enabled = false;
try
{
}
finally
{
monitoringTimer.Enabled = true;
}
}
}
Is it OK?
Shall it work s I expect, i.e. no other thread can now even access resourses, while one is working?
Any disadvantages of such solution?