static private void HashDirectory(System.IO.DirectoryInfo directoryInfo) { FileInfo[] fileInfoSet = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in fileInfoSet) { string current_hash = GetHash(fileInfo.FullName); string key_name = GetKeyName(fileInfo); if (!WatchDictionary.ContainsKey(key_name)) { WatchDictionary.Add(key_name, current_hash); } var worker = new MyTaskWorkerDelegate(MyTaskWorker); worker.BeginInvoke(key_name, current_hash, null, null); } foreach (System.IO.DirectoryInfo di in directoryInfo.GetDirectories()) { string directoryName = di.FullName; System.Console.WriteLine(directoryName); HashDirectory(di); } }
static void fs_Changed(object sender, FileSystemEventArgs e) { FileInfo f = new FileInfo(e.FullPath); if (WatchedFiles.Contains(f.Name)) { try { string current_hash = GetHash(f.FullName); string key_name = GetKeyName(f); if (!WatchDictionary.ContainsKey(key_name)) { WatchDictionary.Add(key_name, current_hash); } if (current_hash != WatchDictionary[key_name]) { var worker = new MyTaskWorkerDelegate(MyTaskWorker); worker.BeginInvoke(key_name, current_hash, null, null); } } catch (System.Exception ex) { System.Console.Write("FileChanged exception:\n {0}", ex); } } }
// Async version of my actual work function public void MyTaskAsync(string[] files) { MyTaskWorkerDelegate worker = new MyTaskWorkerDelegate(MyTaskWorker); AsyncCallback completedCallback = new AsyncCallback(MyTaskCompletedCallback); lock (_sync) { if (_myTaskIsRunning) { throw new InvalidOperationException("The control is currently busy."); } AsyncOperation async = AsyncOperationManager.CreateOperation(null); worker.BeginInvoke(files, completedCallback, async); _myTaskIsRunning = true; } }
public void MyTaskAsync(string[] files) { MyTaskWorkerDelegate worker = new MyTaskWorkerDelegate(MyTaskWorker); AsyncCallback completedCallback = new AsyncCallback(MyTaskCompletedCallback); lock (_sync) { if (_myTaskIsRunning) throw new InvalidOperationException("The control is currently busy."); AsyncOperation async = AsyncOperationManager.CreateOperation(null); MyAsyncContext context = new MyAsyncContext(); bool cancelled; worker.BeginInvoke(files, async, context, out cancelled, completedCallback, async); _myTaskIsRunning = true; _myTaskContext = context; } }
private void MyTaskCompletedCallback(IAsyncResult ar) { // get the original worker delegate and the AsyncOperation instance MyTaskWorkerDelegate worker = (MyTaskWorkerDelegate)((AsyncResult)ar).AsyncDelegate; AsyncOperation async = (AsyncOperation)ar.AsyncState; // finish the asynchronous operation worker.EndInvoke(ar); // clear the running task flag lock (_sync) { _myTaskIsRunning = false; } // raise the completed event AsyncCompletedEventArgs completedArgs = new AsyncCompletedEventArgs(null, false, null); async.PostOperationCompleted( delegate(object e) { OnMyTaskCompleted((AsyncCompletedEventArgs)e); }, completedArgs); }