public void ExtractEmailsAsync() { while (!isTerminating) { // Check if the folder exists and contains atleast 1 text file if (TotalFiles(Scrapers.Pastebin.SaveLocation) == 0) { return; } // Create a worker and a call back MergeWorkerDelegate worker = new MergeWorkerDelegate(ExtractEmailWorker); AsyncCallback completedCallback = new AsyncCallback(MergeCompletedCallBack); // Lock the operation and fire up the worker lock (_sync) { if (isMergeRunning) { throw new InvalidOperationException("Merge is already running, please wait till current merge completes"); } AsyncOperation async = AsyncOperationManager.CreateOperation(null); worker.BeginInvoke(completedCallback, async); isMergeRunning = true; } break; } }
private void MergeCompletedCallBack(IAsyncResult ar) { // Get the original worker delegate instance and async operation MergeWorkerDelegate worker = (MergeWorkerDelegate)((AsyncResult)ar).AsyncDelegate; AsyncOperation async = (AsyncOperation)ar.AsyncState; // Invoke worker end worker.EndInvoke(ar); // Clear the running task flag lock (_sync) { isMergeRunning = false; } // Raise a completion event AsyncCompletedEventArgs completedArgs = new AsyncCompletedEventArgs(null, false, null); async.PostOperationCompleted(delegate(object e) { OnMergeCompleted((AsyncCompletedEventArgs)e); }, completedArgs); }
public void MergeFilesAsync(int byteSize, string location) { while (!isTerminating) { // Check if the folder exists and contains atleast 1 text file if (TotalFiles(location) == 0) { return; } // Generate a file named after the first object in files try { string path = string.Format(@"{0}\Merge{1}bytes", location, byteSize); for (int i = 0; i < 1000; i++) { if (i == 0) { if (File.Exists(path + ".txt")) { continue; } else { path = path + ".txt"; FileStream f = File.Create(path); f.Close(); break; } } if (File.Exists(path + i + ".txt")) { continue; } else { path = path + i + ".txt"; FileStream f = File.Create(path); f.Close(); break; } } destinationPath = location; destinationFile = path; } catch (Exception e) { Console.WriteLine("Failed to create a merge file with reason: " + e); return; } // Create a worker and a call back MergeWorkerDelegate worker = new MergeWorkerDelegate(MergeWorker); AsyncCallback completedCallback = new AsyncCallback(MergeCompletedCallBack); // Lock the operation and fire up the worker lock (_sync) { if (isMergeRunning) { throw new InvalidOperationException("Merge is already running, please wait till current merge completes"); } AsyncOperation async = AsyncOperationManager.CreateOperation(null); worker.BeginInvoke(byteSize, completedCallback, async); isMergeRunning = true; } break; } }