I have a WPF application where I need to save an image to the file system, but I was told to be careful for file share violations. The image keeps getting replaced in a C# DispatcherTimer. I was told to use Thread.Sleep in order to desynchronize the loop. I have several DispatcherTimers running to create images. Is this a proper solution?
private void SaveImage(Image image, string fileToSave, ImageFormat imageFormat, TimeSpan interval)
{
try
{
image.Save(fileToSave, imageFormat);
}
catch (Exception ex)
{
Trace.TraceError("View Capture Service exception on first attempt to save image: " + ex.Message);
Thread.Sleep(Convert.ToInt32(interval.TotalMilliseconds / 2));
image.Save(fileToSave, imageFormat);
}
}