private void DownloadCompleted(object sender, TaskEventArgs e) { string message = ""; if (e.Error != null) { message = "Error downloading: " + e.Error.Message; } else { message = "Downloaded.\nResult:"; string txtFilePath = e.UserState as string; IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream file = storage.OpenFile(txtFilePath, FileMode.Open, FileAccess.Read); using (StreamReader reader = new StreamReader(file)) { message += reader.ReadToEnd(); } } Dispatcher.BeginInvoke(() => { displayMessage(message); } ); }
private void DownloadCompleted(object sender, TaskEventArgs e) { UserTask task = e.UserState as UserTask; if (e.Error != null) { task.TaskStatus = "Downloading error"; task.OutputFilePath = "<error>"; task.ErrorMessage = e.Error.Message; moveTaskToCompleted(task); return; } if (task.IsFieldLevel) { task.RecognizedText = FieldLevelXml.ReadText(task.OutputFilePath); } task.TaskStatus = "Ready"; moveTaskToCompleted(task); }
private void ProcessingCompleted(object sender, TaskEventArgs e) { UserTask task = e.UserState as UserTask; if (task.SourceIsTempFile) { File.Delete(task.SourceFilePath); } if (e.Error != null) { task.TaskStatus = "Processing error"; task.OutputFilePath = "<error>"; task.ErrorMessage = e.Error.Message; moveTaskToCompleted(task); return; } if (e.Result.Status == TaskStatus.NotEnoughCredits) { task.TaskStatus = "Not enough credits"; task.OutputFilePath = "<not enough credits>"; MessageBox.Show("Not enough credits to process the file.\nPlease add more pages to your application's account.", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation); moveTaskToCompleted(task); return; } if (e.Result.Status != TaskStatus.Completed) { task.TaskStatus = "Internal server error"; moveTaskToCompleted(task); return; } task.TaskStatus = "Downloading"; // Start downloading restClientAsync.DownloadFileAsync(e.Result, task.OutputFilePath, task); }
private void onDownloadFileCompleted(object sender, TaskEventArgs e) { if (DownloadFileCompleted != null) { DownloadFileCompleted(sender, e); } }
private void ProcessingCompleted(object sender, TaskEventArgs e) { if (e.Error != null) { Dispatcher.BeginInvoke(() => { displayMessage("Processing error: " + e.Error.Message); } ); return; } Dispatcher.BeginInvoke(() => { displayMessage("Processing completed. Downloading.."); } ); // Download a file string outputPath = "result.txt"; Task task = e.Result; abbyyClient.DownloadFileAsync(task, outputPath, outputPath); }
private void downloadCompletionMethod( Task task, Exception exception, bool canceled, AsyncOperation asyncOp) { if (!canceled) { lock (downloadJobs) { downloadJobs.Remove(asyncOp.UserSuppliedState); } } TaskEventArgs e = new TaskEventArgs(task, exception, canceled, asyncOp.UserSuppliedState); asyncOp.PostOperationCompleted(onDownloadCompletedDelegate, e); }
private void onProcessingCompleted(object sender, TaskEventArgs e) { if (TaskProcessingCompleted != null) { TaskProcessingCompleted(sender, e); } }
private void processFieldWorker(string filePath, IProcessingSettings settings, AsyncOperation asyncOp) { Exception e = null; Task task = new Task(); try { if (settings is TextFieldProcessingSettings) { task = _syncClient.ProcessTextField(filePath, settings as TextFieldProcessingSettings); } else if (settings is BarcodeFieldProcessingSettings) { task = _syncClient.ProcessBarcodeField(filePath, settings as BarcodeFieldProcessingSettings); } else if (settings is CheckmarkFieldProcessingSettings) { task = _syncClient.ProcessCheckmarkField(filePath, settings as CheckmarkFieldProcessingSettings); } else { throw new ArgumentException("Invalid type of processing settings"); } // Notify that upload was completed Task uploadedTask = new Task(task.Id, TaskStatus.Submitted); UploadCompletedEventArgs uploadCompletedEventArgs = new UploadCompletedEventArgs(uploadedTask, asyncOp.UserSuppliedState); asyncOp.Post(onUploadCompletedDelegate, uploadCompletedEventArgs); // Wait until task finishes startTaskMonitorIfNecessary(); _taskList.AddTask(task); task = waitUntilTaskFinishes(task); } catch (Exception ex) { e = ex; } lock (processJobs) { processJobs.Remove(asyncOp.UserSuppliedState); } bool canceled = false; // Package the results of the operation in EventArgs TaskEventArgs ev = new TaskEventArgs(task, e, canceled, asyncOp.UserSuppliedState); // End the task. The asyncOp object is responsible // for marshaling the call. asyncOp.PostOperationCompleted(onProcessingCompleteDelegate, ev); }
// This is the method that the underlying, free-threaded // asynchronous behavior will invoke. This will happen on // a worker thread private void processCompletionMethod( Task task, Exception exception, bool canceled, AsyncOperation asyncOp) { // If the task was not previously canceled, // remove the task from the lifetime collection. if (!canceled) { lock (processJobs) { processJobs.Remove(asyncOp.UserSuppliedState); } } // Package the results of the operation in EventArgs TaskEventArgs e = new TaskEventArgs(task, exception, canceled, asyncOp.UserSuppliedState); // End the task. The asyncOp object is responsible // for marshaling the call. asyncOp.PostOperationCompleted(onProcessingCompleteDelegate, e); // Note that after the call to OperationCompleted, // asyncOp is no longer usable, and any attempt to use it // will cause an exception to be thrown. }
private void processingCompleted(object operationState) { TaskEventArgs e = operationState as TaskEventArgs; onProcessingCompleted(null, e); }
private void downloadCompleted(object operationState) { TaskEventArgs e = operationState as TaskEventArgs; onDownloadFileCompleted(null, e); }
private void ProcessingCompleted(object sender, TaskEventArgs e) { UserTask task = e.UserState as UserTask; if (task.SourceIsTempFile) { File.Delete(task.SourceFilePath); } if (e.Error != null) { task.TaskStatus = "Processing error"; task.OutputFilePath = "<error>"; task.ErrorMessage = e.Error.Message; moveTaskToCompleted(task); return; } if (e.Result.Status == TaskStatus.NotEnoughCredits) { task.TaskStatus = "Not enough credits"; moveTaskToCompleted(task); return; } if (e.Result.Status != TaskStatus.Completed) { task.TaskStatus = "Internal server error"; moveTaskToCompleted(task); return; } task.TaskStatus = "Downloading"; // Start downloading restClientAsync.DownloadFileAsync(e.Result, task.OutputFilePath, task); }
/// <summary> /// Submit and process image asynchronously. /// Performs callbacks: /// UploadFileCompleted /// TaskProcessingCompleted /// </summary> /// <param name="filePath">Path to file in isolated storage</param> /// <param name="settings"></param> /// <param name="userState"></param> public void ProcessImageAsync(string filePath, ProcessingSettings settings, object userState) { BackgroundWorker w = new BackgroundWorker(); w.DoWork += new DoWorkEventHandler((sender, e) => { Task task = null; try { task = _syncClient.ProcessImage(filePath, settings); UploadCompletedEventArgs uploadArgs = new UploadCompletedEventArgs(task, userState); onUploadFileCompleted(sender, uploadArgs); // Wait until task finishes while (true) { task = _syncClient.GetTaskStatus(task.Id); if (!task.IsTaskActive()) { break; } Thread.Sleep(1000); } TaskEventArgs taskArgs = new TaskEventArgs(task, null, false, userState); onProcessingCompleted(sender, taskArgs); } catch (Exception ex) { TaskEventArgs taskArgs = new TaskEventArgs(task, ex, false, userState); onProcessingCompleted(sender, taskArgs); } } ); w.RunWorkerAsync(); }
/// <summary> /// Download file asynchronously /// Performs DownloadFileCompleted callback /// </summary> public void DownloadFileAsync(Task task, string outputPath, object userState) { BackgroundWorker w = new BackgroundWorker(); w.DoWork += new DoWorkEventHandler((sender, e) => { try { _syncClient.DownloadResult(task, outputPath); TaskEventArgs taskArgs = new TaskEventArgs(task, null, false, userState); onDownloadFileCompleted(sender, taskArgs); } catch (Exception ex) { TaskEventArgs taskArgs = new TaskEventArgs(task, ex, false, userState); onDownloadFileCompleted(sender, taskArgs); } } ); w.RunWorkerAsync(); }
public void DownloadCompleted(object sender, TaskEventArgs e) { Char type = 'D'; string text; StoreData store = new StoreData(); MongoDBService.Transaction p = new MongoDBService.Transaction(); if (e.Error != null) { message = "Error downloading: " + e.Error.Message; } else { string txtFilePath = e.UserState as string; IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream file = storage.OpenFile(txtFilePath, FileMode.Open, FileAccess.Read); IsolatedStorageFileStream fileStream = storage.OpenFile(txtFilePath, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(file); text = reader.ReadToEnd(); if (text.Contains("WALGREENS")) type = 'W'; else if (text.Contains("Tops")) type = 'T'; switch (type) { case 'W': using (StreamReader newReader = new StreamReader(fileStream)) { store.Address = newReader.ReadLine() + newReader.ReadLine(); store.StoreName = "WALGREENS"; //Get for storeid from DB newReader.ReadLine(); newReader.ReadLine(); newReader.ReadLine(); newReader.ReadLine(); while (true) { p.Name = newReader.ReadLine(); if (p.Name.Equals("SUBTOTAL")) break; p.ID = double.Parse(newReader.ReadLine()); newReader.ReadLine(); p.quantity = newReader.ReadLine(); p.cost = double.Parse(newReader.ReadLine()); insertData(p); } } break; case 'T': using (StreamReader newReader = new StreamReader(file)) { while (!newReader.ReadLine().Contains("Tops Market")) ; store.Address = newReader.ReadLine() + newReader.ReadLine(); store.StoreName = "WALGREENS"; //get the storeid from DB newReader.ReadLine(); newReader.ReadLine(); newReader.ReadLine(); newReader.ReadLine(); } break; } } }
/// <summary> /// Submit and process image asynchronously. /// Performs callbacks: /// UploadFileCompleted /// TaskProcessingCompleted /// </summary> /// <param name="filePath">Path to file in isolated storage</param> /// <param name="settings"></param> /// <param name="userState"></param> public void ProcessImageAsync(string filePath, ProcessingSettings settings, object userState) { BackgroundWorker w = new BackgroundWorker(); w.DoWork += new DoWorkEventHandler((sender, e) => { OcrSdkTask task = null; try { task = _syncClient.ProcessImage(filePath, settings); UploadCompletedEventArgs uploadArgs = new UploadCompletedEventArgs(task, userState); onUploadFileCompleted(sender, uploadArgs); // Wait until task finishes while (true) { task = _syncClient.GetTaskStatus(task.Id); if (!task.IsTaskActive()) { break; } Thread.Sleep(1000); } if (task.Status == TaskStatus.NotEnoughCredits) { throw new Exception("Not enough credits to process image. Please add more pages to your application's account."); } TaskEventArgs taskArgs = new TaskEventArgs(task, null, false, userState); onProcessingCompleted(sender, taskArgs); } catch (Exception ex) { TaskEventArgs taskArgs = new TaskEventArgs(task, ex, false, userState); onProcessingCompleted(sender, taskArgs); } } ); w.RunWorkerAsync(); }