My application currently executes Adobe Illustrator with some command. Waits when result file appears in some exact folder (with async function) and does something with file when it's ready.
But the problem is, sometimes Adobe Illustrator fails and app keeps waiting. In such cases I can't figure out, how can I apply timeout mechanism to kill Adobe Illustrator and skip current process.
Here is the code:
...
await WhenFileCreated(result_file_name);
if (File.Exists(result_file_name))
{
...
public static Task WhenFileCreated(string path)
{
if (File.Exists(path))
return Task.FromResult(true);
var tcs = new TaskCompletionSource<bool>();
FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(path));
FileSystemEventHandler createdHandler = null;
RenamedEventHandler renamedHandler = null;
createdHandler = (s, e) =>
{
if (e.Name == Path.GetFileName(path))
{
tcs.TrySetResult(true);
watcher.Created -= createdHandler;
watcher.Dispose();
}
};
renamedHandler = (s, e) =>
{
if (e.Name == Path.GetFileName(path))
{
tcs.TrySetResult(true);
watcher.Renamed -= renamedHandler;
watcher.Dispose();
}
};
watcher.Created += createdHandler;
watcher.Renamed += renamedHandler;
watcher.EnableRaisingEvents = true;
return tcs.Task;
}
How to apply timeout to this? Any suggestions?