public static bool DirectoryExists(string path, int millisecondsTimeout = 5000) { try { var callback = new DirectoryExistsDelegate(Directory.Exists); var result = callback.BeginInvoke(path, null, null); if (result.AsyncWaitHandle.WaitOne(millisecondsTimeout, false)) { return(callback.EndInvoke(result)); } else { callback.EndInvoke(result); // Needed to terminate thread? return(false); } } catch (Exception) { return(false); } }
bool DirectoryExistsTimeout(string path, int millisecondsTimeout) { try { DirectoryExistsDelegate callback = new DirectoryExistsDelegate(Directory.Exists); IAsyncResult result = callback.BeginInvoke(path, null, null); if (result.AsyncWaitHandle.WaitOne(millisecondsTimeout, false)) { return(callback.EndInvoke(result)); } else { callback.EndInvoke(result); return(false); } } catch (Exception) { return(false); } }
static bool DirectoryExistsTimeout(string path, int millisecondsTimeout) { try { DirectoryExistsDelegate callback = new DirectoryExistsDelegate(Directory.Exists); IAsyncResult result = callback.BeginInvoke(path, null, null); if (result.AsyncWaitHandle.WaitOne(millisecondsTimeout, false)) { return callback.EndInvoke(result); } else { //callback.EndInvoke(result); // Problem: this seems to make the current thread block for response! return false; } } catch (Exception) { return false; } }
bool DirectoryExistsTimeout(string path, int millisecondsTimeout) { try { DirectoryExistsDelegate callback = new DirectoryExistsDelegate(Directory.Exists); IAsyncResult result = callback.BeginInvoke(path, null, null); if (result.AsyncWaitHandle.WaitOne(millisecondsTimeout, false)) { return callback.EndInvoke(result); } else { callback.EndInvoke(result); return false; } } catch (Exception) { return false; } }