Hello Everyone,
I am currently using filesystemwatcher to transfer text files from my local machine onto a different server. The files loaded onto my local machine can be loaded at anytime of the day, and the problem is the server I am moving it to is known to go down several times.
Whenever a file is created on my local machine I want it to be copied directly to the server, and if the server is down, I want it to continually try every 5-10 minutes until successfull.
My code is the following for when a file is created
private void watch_Created(object sender, System.IO.FileSystemEventArgs e)
{
string copytoFolder = @""+foldercpPath.Text;
//If the given directory does not exist wait for 2 minutes and check again.
while (!Directory.Exists(copytoFolder))
{
textBox1.Text += e.Name +
" is waiting to be copied...\n\n";
Delay(120000); // I can also use thread.sleep(120000) but this is just as inefficient.
}
File.Copy(e.FullPath, copytoFolder + e.Name);
textBox1.Text += e.Name +
" Successfully Copied\n\n";
}
public
static DateTime Delay(int MilliSecondsToPauseFor)
{
DateTime ThisMoment = DateTime.Now;
TimeSpan duration = new TimeSpan(0, 0, 0, 0, MilliSecondsToPauseFor);
DateTime AfterWards = ThisMoment.Add(duration);
while (AfterWards >= ThisMoment)
{
Application.DoEvents();
ThisMoment =
DateTime.Now;
}
return DateTime.Now;
}
Is there a way to put in a 2 minute delay without using the sleep method or making a while loop? I'm trying to make this as efficient as possible. I was told I could put my application to sleep and have the filesystemwatcher make a wakeup call everytime a new file is created. Is this possible? How would I do it?
Thank you all very much for any help you can provide.