//return true if there are more steps public bool ExecuteStep(SocketMessage Message) { ProcessStep Step; if (ProcessSteps.TryDequeue(out Step)) { Step.Execute(Message, this); if (OnProcessStep != null) { OnProcessStep.Invoke(Message, this, Step); } } bool MoreSteps = ProcessSteps.Count > 0; if (!MoreSteps) { if (OnProcessComplete != null) { OnProcessComplete.Invoke(this); } } return(MoreSteps); }
private void ReadMatrixFromFlow_Direct() { try { if (MatrixString.Length >= 384) { string matrix_correct = ""; int i = 0; for (i = 0; i < 384; ++i) { if (i % 48 == 0) { matrix_correct += Environment.NewLine; } matrix_correct += MatrixString[i]; } MatrixString = MatrixString.Substring(i); var matrix = ProcessMatrix(matrix_correct); OnProcessComplete?.Invoke(matrix); InvokeOnMainThread(() => textBox2.Text = stopWatch.ElapsedMilliseconds + " ms"); stopWatch.Restart(); return; } } catch (Exception e) { OnError(e); } }
private void ReadMatrixFromFlow(string read) { try { if (read.IndexOf("BeginMatrix") != -1) { MatrixString = ""; return; } if (read.IndexOf("EndMatrix") != -1) { var matrix = ProcessMatrix(MatrixString); OnProcessComplete?.Invoke(matrix); InvokeOnMainThread(() => textBox2.Text = stopWatch.ElapsedMilliseconds + " ms"); stopWatch.Restart(); return; } MatrixString += read + Environment.NewLine; } catch (Exception e) { OnError(e); } }
/// <summary> /// Main method - run the batch /// </summary> /// <returns></returns> public async Task Run() { foreach (object item in Items) { // Await all file reads object result = await BatchSingle(item); // Check completion Completed++; BatchCompletedEventArgs frea = new BatchCompletedEventArgs() { Result = result }; OnProcessComplete?.Invoke(this, frea); if (Completed == Amount) { frea = new BatchCompletedEventArgs(); OnBatchComplete?.Invoke(this, frea); } } }
private void ApplyFilterMultithreaded(IImageFilter imageFilter, EdgeHandling edgeHandling) { int numberOfThreads = Environment.ProcessorCount; // ReSharper disable once InconsistentlySynchronizedField _updateProgressDictionary.Clear(); try { _sourceDataBase = new SourceDataBase(_sourceBitmap, edgeHandling); int imgPixelSize = _sourceDataBase.NumberOfPixels; int workSize = Convert.ToInt32(Math.Floor(imgPixelSize / (double)numberOfThreads)); int remaningPixels = imgPixelSize % numberOfThreads; //Split work into number of threads awailable for this system var doneEvents = new ManualResetEvent[numberOfThreads]; processFilterArray = new ProcessFilter[numberOfThreads]; int startIndex = 0; for (int i = 0; i < numberOfThreads; i++) { doneEvents[i] = new ManualResetEvent(false); ProcessFilter processFilter; var sourceData = new SourceData(_sourceDataBase); if (remaningPixels > 0 && i == numberOfThreads - 1) { processFilter = new ProcessFilter(sourceData, imageFilter, startIndex, startIndex + workSize + remaningPixels, doneEvents[i]); } else { processFilter = new ProcessFilter(sourceData, imageFilter, startIndex, startIndex + workSize, doneEvents[i]); } processFilter.OnProgressUpdate += processFilter_OnProgressUpdate; startIndex += workSize; processFilterArray[i] = processFilter; ThreadPool.QueueUserWorkItem(processFilter.ThreadPoolCallback, i); } waitHandleArray = new WaitHandle[doneEvents.Length]; for (int i = 0; i < doneEvents.Length; i++) { waitHandleArray[i] = doneEvents[i]; } // Wait for all threads in pool to complete. WaitHandle.WaitAll(waitHandleArray); _outputBitmap = _sourceDataBase.GetOutputBitmap(); } catch (Exception ex) { string message = ex.Message; Console.WriteLine(message); } processFilterArray = null; waitHandleArray = null; IsRunning = false; if (OnProcessComplete != null) { OnProcessComplete.Invoke(this, new EventArgs()); } }