I'm using FileSystemWatcher. I'm calling the WatchDirectory from a button click event. Then i want assign to label6.Text once the file is busy to display "busy" and when the file is not busy any more to display "not busy".
And using async i'm not sure if it's the right way here. This wait the methods are i'm getting errors.
On WatchDirectory i'm getting the error:
Cannot find all types required by the 'async' modifier. Are you targeting the wrong framework version, or missing a reference to an assembly?
Same error on the line: return await tcs.Task;
On WaitForUnlockedFile i'm getting the error:
Cannot find all types required by the 'async' modifier. Are you targeting the wrong framework version, or missing a reference to an assembly?
And last error on :
await Task.Delay(100);
'System.Threading.Tasks.Task' does not contain a definition for 'Delay'
private async Task<string> WatchDirectory()
{
using (FileSystemWatcher watcher = new FileSystemWatcher())
{
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
watcher.Path = SavePathTextBox.Text;
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
watcher.Filter = "*.jpg";
watcher.Changed += (sender, e) =>
{
if (e.ChangeType == WatcherChangeTypes.Changed)
{
var info = new FileInfo(e.FullPath);
var theSize = info.Length;
label5.BeginInvoke((Action)(() =>
{
label6.Text = theSize.ToString();
}));
}
tcs.SetResult(e.FullPath);
};
watcher.EnableRaisingEvents = true;
return await tcs.Task;
}
}
And the WaitForUnlockedFile method
private async Task WaitForUnlockedFile(string fileName)
{
while (true)
{
try
{
using (IDisposable stream = File.Open(fileName, FileMode.OpenOrCreate,
FileAccess.ReadWrite, FileShare.None))
{ /* on success, immediately dispose object */ }
break;
}
catch (IOException)
{
}
await Task.Delay(100);
}
}