private void onChanged(object source, FileSystemEventArgs e)
{
// Wait until the file is writeable
while (true)
{
try
{
using (Stream stream = File.Open(currentFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
if (stream != null)
break;
}
}
catch (FileNotFoundException ex)
{ }
catch (IOException ex)
{ }
catch (UnauthorizedAccessException ex)
{ }
Thread.Sleep(500);
}
// Notify the form that the collection has been changed, but only if it's been
// at least two seconds since last update
if ((lastUpdated == null || (DateTime.Now - lastUpdated).TotalSeconds > 2) && Properties.Settings.Default.syncCollection)
{
callbackFunction();
lastUpdated = DateTime.Now;
}
}