/// <summary> /// [可重入]取消后台操作 /// </summary> /// <param name="bw"></param> public static void CancelAsync(System.ComponentModel.BackgroundWorker bw) { if (bw.IsBusy && !bw.CancellationPending) { bw.CancelAsync(); } }
/// <summary> /// This method checks the given <see cref="AOIData"/> /// if it is already used in the database. /// </summary> /// <param name="shape">Ref. A <see cref="AOIData"/> with /// the area of interest to test.</param> /// <param name="splashDialog">A <see cref="System.ComponentModel.BackgroundWorker"/> with the /// background splash loading dialog worker which shows progress.</param> /// <returns><strong>True</strong>, if an AOI with same name and stimulus assignment /// already exists in the database, /// otherwise <strong>false</strong>.</returns> private static bool CheckForAOIDuplicate(AOIData shape, System.ComponentModel.BackgroundWorker splashDialog) { SQLiteOgamaDataSet.AOIsDataTable aoisTable = Document.ActiveDocument.DocDataSet.AOIsAdapter.GetDataByTrialIDAndShapeName(shape.TrialID, shape.ShapeName); if (aoisTable.Rows.Count > 0) { if (splashDialog != null && splashDialog.IsBusy) { splashDialog.CancelAsync(); } string message = "The area of interest " + Environment.NewLine + shape.ShapeType.ToString() + ": " + shape.ShapeName + "already exists." + Environment.NewLine + "Would you like to overwrite it ?"; DialogResult duplicateQuestion = InformationDialog.Show("Overwrite AOI ?", message, true, MessageBoxIcon.Question); // Show loading splash screen if it is not running if (splashDialog != null && !splashDialog.IsBusy) { splashDialog.RunWorkerAsync(); } if (duplicateQuestion == DialogResult.No || duplicateQuestion == DialogResult.Cancel) { return false; } // User clicked yes so update the existing entry // Presuming the DataTable has a column named ShapeName. string expression = "ShapeName = '" + shape.ShapeName + "' AND TrialID = '" + shape.TrialID + "'"; DataRow[] foundRows; // Use the Select method to find all rows matching the filter. foundRows = Document.ActiveDocument.DocDataSet.AOIs.Select(expression); if (foundRows.Length > 0) { foreach (DataRow row in foundRows) { row["ShapeType"] = shape.ShapeType; row["ShapeNumPts"] = shape.ShapeNumPts; row["ShapePts"] = shape.ShapePts; } } return false; } return true; }
/// <summary> /// This method checks the given <see cref="AOIData"/> shape for invalid characters /// and tests if it is already used in the database. /// It shows referring message boxes when the name is not valid /// and automatically removes invalid characters. /// </summary> /// <param name="shape">Ref. A <see cref="AOIData"/> with /// the area of interest to test.</param> /// <param name="splashDialog">A <see cref="System.ComponentModel.BackgroundWorker"/> with the /// background splash loading dialog worker which shows progress.</param> /// <returns><strong>True</strong>, if the AOI is valid, /// otherwise <strong>false</strong>.</returns> public static bool ValidateShape(ref AOIData shape, System.ComponentModel.BackgroundWorker splashDialog) { shape.ShapeName.Trim(); StringBuilder sb = new StringBuilder(); bool changed = false; for (int i = 0; i < shape.ShapeName.Length; i++) { if (char.IsLetterOrDigit(shape.ShapeName[i])) { sb.Append(shape.ShapeName[i]); } else { changed = true; } } shape.ShapeName = sb.ToString(); if (shape.ShapeName == string.Empty) { if (splashDialog != null && splashDialog.IsBusy) { splashDialog.CancelAsync(); } InformationDialog.Show( "Empty shape name", "Please enter at least one character for the shape name", false, MessageBoxIcon.Information); // Show loading splash screen if it is not running if (splashDialog != null && !splashDialog.IsBusy) { splashDialog.RunWorkerAsync(); } return false; } else if (IOHelpers.IsNumeric(shape.ShapeName[0])) { if (splashDialog != null && splashDialog.IsBusy) { splashDialog.CancelAsync(); } InformationDialog.Show( "Invalid shape name", "Please do not use a number for the first character of the shape name.", false, MessageBoxIcon.Information); // Show loading splash screen if it is not running if (splashDialog != null && !splashDialog.IsBusy) { splashDialog.RunWorkerAsync(); } return false; } else { if (changed) { if (splashDialog != null && splashDialog.IsBusy) { splashDialog.CancelAsync(); } string message = "Please note: Non letter and non digit characters are removed from the shape name." + Environment.NewLine + "Other characters are not allowed for the database entry."; ExceptionMethods.ProcessMessage("Shape name modified", message); // Show loading splash screen if it is not running if (splashDialog != null && !splashDialog.IsBusy) { splashDialog.RunWorkerAsync(); } return true; } if (!CheckForAOIDuplicate(shape, splashDialog)) { return false; } } return true; }
public static bool recvEncryptedFile(long size, Socket s, AesCryptoServiceProvider key, String path, System.ComponentModel.BackgroundWorker worker) { Int64 left = size; byte[] received = new byte[size]; try { using (FileStream writer = File.Create(path)) { while (left > 0) { int dim = (left > 4096) ? 4096 : (int)left; dim = (dim % 16 == 0) ? dim + 16 : dim + (16 - (dim % 16)); byte[] buffer = my_recv(dim, s); if (buffer == null) { worker.CancelAsync(); return false; } byte[] decryptedData = Security.AESDecrypt(key, buffer); if (decryptedData == null) { worker.CancelAsync(); return false; } writer.Write(decryptedData, 0, decryptedData.Length); left -= decryptedData.Length; double percentage = (double)decryptedData.Length / size; worker.ReportProgress((int)(percentage * 100)); } return true; } } catch (SocketException) { worker.CancelAsync(); } catch (IOException) { //IO error : disk full? worker.CancelAsync(); } return false; }
private void InitializeBgW(ref System.ComponentModel.BackgroundWorker bgw, System.ComponentModel.DoWorkEventHandler dweh, RunWorkerCompletedEventHandler rwceh) { if (bgw != null) { if (bgw.IsBusy) { if (!bgw.CancellationPending) { bgw.CancelAsync(); bgw.Dispose(); } } } bgw = new System.ComponentModel.BackgroundWorker(); bgw.WorkerSupportsCancellation = true; bgw.DoWork += dweh; bgw.RunWorkerCompleted += rwceh; }