/// <summary> /// This test cancels a Parallel.* loop in flight and checks that we get a OperationCanceledException. /// </summary> /// <returns></returns> internal void RealRun() { ParallelLoopResult result = new ParallelLoopResult(); CancellationTokenSource cts = new CancellationTokenSource(); var allowCancel = new ManualResetEventSlim(); var wakeLoop = new ManualResetEventSlim(); Action body = () => { allowCancel.Set(); wakeLoop.Wait(); }; Task wrappedTask = Task.Factory.StartNew(delegate { CancellationToken ct = cts.Token; switch (_api) { case API.For: result = Parallel.For(0, Int32.MaxValue, new ParallelOptions() { CancellationToken = ct }, (i) => body()); break; case API.For64: result = Parallel.For(0, Int64.MaxValue, new ParallelOptions() { CancellationToken = ct }, (i) => body()); break; case API.Foreach: result = Parallel.ForEach<int>(GetIEnumerable(), new ParallelOptions() { CancellationToken = ct }, (i) => body()); break; } }, cts.Token, TaskCreationOptions.None, TaskScheduler.Default); allowCancel.Wait(); cts.Cancel(); wakeLoop.Set(); Assert.ThrowsAny<OperationCanceledException>(() => wrappedTask.GetAwaiter().GetResult()); // verify result : Stopped <==> if Completed is false and LowestBreakIteration == null Assert.False(result.IsCompleted, "Should not be completed."); Assert.Null(result.LowestBreakIteration); }
/// <summary> /// This test cancels a Parallel.* loop in flight and checks that we get a OperationCanceledException / TaskCanceledException /// </summary> /// <returns></returns> internal void RealRun() { ParallelLoopResult result = new ParallelLoopResult(); CancellationTokenSource cts = new CancellationTokenSource(); Task wrappedTask = Task.Factory.StartNew(delegate { CancellationToken ct = cts.Token; switch (_api) { case API.For: result = Parallel.For(0, Int32.MaxValue, new ParallelOptions() { CancellationToken = ct }, (i) => { }); break; case API.For64: result = Parallel.For(0, Int64.MaxValue, new ParallelOptions() { CancellationToken = ct }, (i) => { }); break; case API.Foreach: result = Parallel.ForEach<int>(GetIEnumerable(), new ParallelOptions() { CancellationToken = ct }, (i) => { }); break; } }, cts.Token, TaskCreationOptions.None, TaskScheduler.Default); var task = Task.Delay(Max_Cancellation_Delay); task.Wait(); cts.Cancel(); try { wrappedTask.Wait(); } catch (AggregateException ae) { ae.Flatten().Handle((e) => { return e is OperationCanceledException || e is TaskCanceledException; }); } // verify result : Stopped <==> if Completed is false and LowestBreakIteration == null Assert.False(result.IsCompleted, "Should not be completed."); Assert.Null(result.LowestBreakIteration); }
public static void Main() { int[] _specificExercisesToRun = ConsoleTools.GetSpecificExercisesToRun(); if (_specificExercisesToRun.Contains(1) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("1. Thread - Basic"))) { // Would have used 'ThreadStart' here, but because the ThreadMethod gets used several times here, slightly different, // 'ParameterizedThreadStart' is used here (and further on), as to use params. Thread t = new Thread(new ParameterizedThreadStart(ThreadMethod)); t.Start(1); // '1' is the param offered to the ThreadMethod. for (int i = 0; i < 4; i++) { Console.WriteLine("Main thread: Do some work."); Thread.Sleep(1); } t.Join(); } if (_specificExercisesToRun.Contains(2) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("2. Thread - Background"))) { Thread t = new Thread(new ParameterizedThreadStart(ThreadMethod)); t.IsBackground = false; // 'true' only makes sense if it's the last thing the console application has to do. // otherwise, the output will get written anyway. t.Start(100); } if (_specificExercisesToRun.Contains(3) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("3. Tasks"))) { Task <int>[] tasks = new Task <int> [3]; tasks[0] = Task.Run(() => { Thread.Sleep(2000); return(1); }); tasks[1] = Task.Run(() => { Thread.Sleep(1000); return(2); }); tasks[2] = Task.Run(() => { Thread.Sleep(3000); return(3); }); while (tasks.Length > 0) { int i = Task.WaitAny(tasks); Task <int> completedTask = tasks[i]; Console.WriteLine(completedTask.Result); var temp = tasks.ToList(); temp.RemoveAt(i); tasks = temp.ToArray(); } } if (_specificExercisesToRun.Contains(4) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("4. Tasks - Shared variable to stop a thread"))) { bool stopped = false; Thread t = new Thread(new ThreadStart(() => { while (!stopped) { Console.WriteLine("Running..."); Thread.Sleep(1000); } })); t.Start(); Console.WriteLine("Press any key to exit this exercise"); Console.ReadKey(); stopped = true; t.Join(); } if (_specificExercisesToRun.Contains(5) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("5. Thread - Using the ThreadStaticAttribute"))) { new Thread(() => { for (int x = 0; x < 10; x++) { _field01++; Console.WriteLine("Thread A: {0}", _field01); } }).Start(); new Thread(() => { for (int x = 0; x < 10; x++) { _field01++; Console.WriteLine("Thread B: {0}", _field01); } }).Start(); } if (_specificExercisesToRun.Contains(6) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("6. Thread - Using ThreadLocal<T>"))) { new Thread(() => { for (int x = 0; x < _field02.Value; x++) { Console.WriteLine("Thread A: {0}", x); } }).Start(); new Thread(() => { for (int x = 0; x < _field02.Value; x++) { Console.WriteLine("Thread B: {0}", x); } }).Start(); } if (_specificExercisesToRun.Contains(7) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("7. Thread - Queuing some work to the thread pool"))) { ThreadPool.QueueUserWorkItem((s) => { Console.WriteLine("Working on a thread from threadpool"); }); } if (_specificExercisesToRun.Contains(8) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("8. Tasks - How to start a new Task and wait until it’s finished"))) { Task t = Task.Run(() => { for (int x = 0; x < 100; x++) { Console.Write('>'); } }); t.Wait(); } if (_specificExercisesToRun.Contains(9) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("9. Tasks - If a Task should return a value"))) { Task <int> t = Task.Run(() => { return(42); }); Console.WriteLine(t.Result); } if (_specificExercisesToRun.Contains(10) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("10. Tasks - Add a continuation Task"))) { Task <int> t = Task.Run(() => { return(42); }).ContinueWith((i) => { return(i.Result * 2); }); Console.WriteLine(t.Result); } if (_specificExercisesToRun.Contains(11) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("11. Tasks - Scheduling different continuation Tasks"))) { Task <int> t = Task.Run(() => { return(42); }); t.ContinueWith((i) => { Console.WriteLine("Canceled"); }, TaskContinuationOptions.OnlyOnCanceled); t.ContinueWith((i) => { Console.WriteLine("Faulted"); }, TaskContinuationOptions.OnlyOnFaulted); var completedTask = t.ContinueWith((i) => { Console.WriteLine("Completed"); }, TaskContinuationOptions.OnlyOnRanToCompletion); completedTask.Wait(); } if (_specificExercisesToRun.Contains(12) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("12. Tasks - Attaching child Tasks to a parent Task"))) { Task <Int32[]> parent = Task.Run(() => { var results = new Int32[3]; new Task(() => results[0] = 0, TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[1] = 1, TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[2] = 2, TaskCreationOptions.AttachedToParent).Start(); return(results); }); var finalTask = parent.ContinueWith(parentTask => { foreach (int i in parentTask.Result) { Console.WriteLine(i); } }); finalTask.Wait(); } if (_specificExercisesToRun.Contains(13) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("13. Tasks - Using a TaskFactory"))) { Task <Int32[]> parent = Task.Run(() => { var results = new Int32[3]; TaskFactory tf = new TaskFactory(TaskCreationOptions.AttachedToParent, TaskContinuationOptions.ExecuteSynchronously); tf.StartNew(() => results[0] = 0); tf.StartNew(() => results[1] = 1); tf.StartNew(() => results[2] = 2); return(results); }); var finalTask = parent.ContinueWith(parentTask => { foreach (int i in parentTask.Result) { Console.WriteLine(i); } }); finalTask.Wait(); } if (_specificExercisesToRun.Contains(14) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("14. Tasks - Using Task.WaitAll"))) { Task[] tasks = new Task[3]; tasks[0] = Task.Run(() => { Thread.Sleep(1000); Console.WriteLine("1"); return(1); }); tasks[1] = Task.Run(() => { Thread.Sleep(1000); Console.WriteLine("2"); return(2); }); tasks[2] = Task.Run(() => { Thread.Sleep(1000); Console.WriteLine("3"); return(3); }); Task.WaitAll(tasks); } if (_specificExercisesToRun.Contains(15) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("15. Tasks - Using Task.WaitAny"))) { Task <int>[] tasks = new Task <int> [3]; tasks[0] = Task.Run(() => { Thread.Sleep(2000); return(1); }); tasks[1] = Task.Run(() => { Thread.Sleep(1000); return(2); }); tasks[2] = Task.Run(() => { Thread.Sleep(3000); return(3); }); while (tasks.Length > 0) { int i = Task.WaitAny(tasks); Task <int> completedTask = tasks[i]; Console.WriteLine(completedTask.Result); var temp = tasks.ToList(); temp.RemoveAt(i); tasks = temp.ToArray(); } } if (_specificExercisesToRun.Contains(16) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("16. Parallel - Using Parallel.For and Parallel.Foreach"))) { Parallel.For(0, 10, i => { Thread.Sleep(1000); }); var numbers = Enumerable.Range(0, 10); Parallel.ForEach(numbers, i => { Thread.Sleep(1000); }); } if (_specificExercisesToRun.Contains(17) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("17. Parallel - Using Parallel.Break"))) { ParallelLoopResult result = Parallel.For(0, 1000, (int i, ParallelLoopState loopState) => { if (i == 500) { Console.WriteLine("Breaking loop"); loopState.Break(); } return; }); } if (_specificExercisesToRun.Contains(18) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("18. Async and Await - Simple example"))) { // Uses static method DownloadContent string result = DownloadContent().Result; Console.WriteLine(result); } if (_specificExercisesToRun.Contains(19) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("19. Async and Await - PLINQ - Using AsParallel"))) { var numbers = Enumerable.Range(0, 100000000); var parallelResult = numbers.AsParallel() .Where(i => i % 2 == 0) .ToArray(); } if (_specificExercisesToRun.Contains(20) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("20. Async and Await - PLINQ - Unordered parallel query"))) { var numbers = Enumerable.Range(0, 10); var parallelResult = numbers.AsParallel() .Where(i => i % 2 == 0) .ToArray(); foreach (int i in parallelResult) { Console.WriteLine(i); } } if (_specificExercisesToRun.Contains(21) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("21. Async and Await - PLINQ - Ordered parallel query"))) { var numbers = Enumerable.Range(0, 10); var parallelResult = numbers.AsParallel().AsOrdered() .Where(i => i % 2 == 0) .ToArray(); foreach (int i in parallelResult) { Console.WriteLine(i); } } if (_specificExercisesToRun.Contains(22) || (_specificExercisesToRun.Contains(0) && ConsoleTools.Run("22. Async and Await - PLINQ - Making a parallel query sequential"))) { var numbers = Enumerable.Range(0, 20); var parallelResult = numbers.AsParallel().AsOrdered() .Where(i => i % 2 == 0).AsSequential(); foreach (int i in parallelResult.Take(5)) { Console.WriteLine(i); } } /* * if (ConsoleTools.Run("Volgende is van pagina 26 !!!")) * { * var dict = new ConcurrentDictionary<string, int>(); * dict["k1"] = 42; * int r1 = dict.AddOrUpdate("k1", 3, (s, i) => i * 2); * } */ }
static void Main(string[] args) { Task task = new Task(() => //задача на основе task { Console.WriteLine("Введите n"); int n = int.Parse(Console.ReadLine()); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = 1; } for (int i = 2; i < n; i++) { for (int j = i + 1; j < n; j++) { if (j % i == 0) { arr[j] = 0; } else { continue; } } } Console.WriteLine("Напротив простых чисел - 1, напротив остальных - 0"); for (int h = 1; h < n; h++) { Console.WriteLine($"{h} {arr[h]}"); } }); Stopwatch watch = new Stopwatch(); watch.Start(); task.Start(); watch.Stop(); TimeSpan time = watch.Elapsed; Console.WriteLine($"Task {task.Id}: {task.Status}\n"); //идентификатор текущей задачи и статус Console.WriteLine("RunTime " + time); //время выполнения Console.ReadKey(); Console.Clear(); CancellationTokenSource cancellation = new CancellationTokenSource(); CancellationToken token = cancellation.Token; Task task1 = new Task(() => { Console.WriteLine("Введите число n"); int n = int.Parse(Console.ReadLine()); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = 1; } for (int i = 2; i < n; i++) { for (int j = i + 1; j < n; j++) { if (j % i == 0) { arr[j] = 0; } else { continue; } } } if (token.IsCancellationRequested) { Console.WriteLine("Операция прервана"); } else { Console.WriteLine("Напротив простых чисел - 1, напротив остальных - 0"); for (int h = 1; h < n; h++) { Console.WriteLine($"{h} {arr[h]}"); } } }); task1.Start(); Console.WriteLine("Введите Q для отмены операции или другой символ для её продолжения: "); string str = Console.ReadLine(); if (str == "Q") { cancellation.Cancel(); //токен отмены } Console.ReadLine(); Task <int> Task1 = new Task <int>(() => Multi(3, 5)); Task1.Start(); Task <int> Task2 = new Task <int>(() => Divide(4, 2)); Task2.Start(); Task <int> Task3 = new Task <int>(() => Minus(10, 8)); Task3.Start(); Task <int> Task4 = new Task <int>(() => Task1.Result + Task2.Result + Task3.Result); Task4.Start(); Console.WriteLine(Task1.Result); Console.WriteLine(Task2.Result); Console.WriteLine(Task3.Result); Console.WriteLine(Task4.Result); Task <int> tasK1 = new Task <int>(() => Multi(7, 8)); Task tasK2 = tasK1.ContinueWith(mul => Show(mul.Result)); //планировка на основе завершения множества предшествующих задач tasK1.Start(); tasK2.Wait(); Console.ReadLine(); Random rand = new Random(); Task <int> what = Task.Run(() => rand.Next(10) * rand.Next(10)); var awaiter = what.GetAwaiter(); //получаем объект типа awaiter, используемый для данного объекта Task awaiter.OnCompleted(() => //задаем действие выполн. когда awaiter перестает ждать завершения задачи { Console.WriteLine("Result: " + awaiter.GetResult()); }); Console.ReadLine(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Parallel.For(0, 5, BigArr); //распараллеливаем вычисления для for stopwatch.Stop(); TimeSpan span = stopwatch.Elapsed; Console.WriteLine($"Время: {span}"); Console.ReadLine(); List <int> list = new List <int>() { 1, 2, 3, 4, 5 }; ParallelLoopResult result = Parallel.ForEach <int>(list, BigArr); //результат параллельных вычисления для foreach Stopwatch stopwatch1 = new Stopwatch(); stopwatch1.Start(); Parallel.For(0, 5, BigArr); stopwatch1.Stop(); TimeSpan span1 = stopwatch.Elapsed; Console.WriteLine($"Время: {span1}"); Console.ReadLine(); Parallel.Invoke(CurTask, //распараллелить выполнение блоков операторов () => { }, () => Multi(5, 19)); Console.ReadLine(); ArrAsynk(); }
private bool ExecuteActual() { if (SourceFiles.Length == 0) { Log.LogError($"No SourceFiles to compile"); return(false); } ITaskItem?badItem = SourceFiles.FirstOrDefault(sf => string.IsNullOrEmpty(sf.GetMetadata("ObjectFile"))); if (badItem != null) { Log.LogError($"Source file {badItem.ItemSpec} is missing ObjectFile metadata."); return(false); } if (!Enum.TryParse(OutputMessageImportance, ignoreCase: true, out MessageImportance messageImportance)) { Log.LogError($"Invalid value for OutputMessageImportance={OutputMessageImportance}. Valid values: {string.Join(", ", Enum.GetNames(typeof(MessageImportance)))}"); return(false); } _totalFiles = SourceFiles.Length; IDictionary <string, string> envVarsDict = GetEnvironmentVariablesDict(); ConcurrentBag <ITaskItem> outputItems = new(); try { List <(string, string)> filesToCompile = new(); foreach (ITaskItem srcItem in SourceFiles) { string srcFile = srcItem.ItemSpec; string objFile = srcItem.GetMetadata("ObjectFile"); string depMetadata = srcItem.GetMetadata("Dependencies"); string[] depFiles = string.IsNullOrEmpty(depMetadata) ? Array.Empty <string>() : depMetadata.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (!ShouldCompile(srcFile, objFile, depFiles, out string reason)) { Log.LogMessage(MessageImportance.Low, $"Skipping {srcFile} because {reason}."); outputItems.Add(CreateOutputItemFor(srcFile, objFile)); } else { Log.LogMessage(MessageImportance.Low, $"Compiling {srcFile} because {reason}."); filesToCompile.Add((srcFile, objFile)); } } _numCompiled = SourceFiles.Length - filesToCompile.Count; if (_numCompiled == _totalFiles) { // nothing to do! OutputFiles = outputItems.ToArray(); return(!Log.HasLoggedErrors); } if (_numCompiled > 0) { Log.LogMessage(MessageImportance.High, $"[{_numCompiled}/{SourceFiles.Length}] skipped unchanged files"); } Log.LogMessage(MessageImportance.Low, "Using environment variables:"); foreach (var kvp in envVarsDict) { Log.LogMessage(MessageImportance.Low, $"\t{kvp.Key} = {kvp.Value}"); } string workingDir = Environment.CurrentDirectory; Log.LogMessage(MessageImportance.Low, $"Using working directory: {workingDir}"); _tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(_tempPath); int allowedParallelism = DisableParallelCompile ? 1 : Math.Min(SourceFiles.Length, Environment.ProcessorCount); if (BuildEngine is IBuildEngine9 be9) { allowedParallelism = be9.RequestCores(allowedParallelism); } /* * From: https://github.com/dotnet/runtime/issues/46146#issuecomment-754021690 * * Stephen Toub: * "As such, by default ForEach works on a scheme whereby each * thread takes one item each time it goes back to the enumerator, * and then after a few times of this upgrades to taking two items * each time it goes back to the enumerator, and then four, and * then eight, and so on. This ammortizes the cost of taking and * releasing the lock across multiple items, while still enabling * parallelization for enumerables containing just a few items. It * does, however, mean that if you've got a case where the body * takes a really long time and the work for every item is * heterogeneous, you can end up with an imbalance." * * The time taken by individual compile jobs here can vary a * lot, depending on various factors like file size. This can * create an imbalance, like mentioned above, and we can end up * in a situation where one of the partitions has a job that * takes very long to execute, by which time other partitions * have completed, so some cores are idle. But the the idle * ones won't get any of the remaining jobs, because they are * all assigned to that one partition. * * Instead, we want to use work-stealing so jobs can be run by any partition. */ ParallelLoopResult result = Parallel.ForEach( Partitioner.Create(filesToCompile, EnumerablePartitionerOptions.NoBuffering), new ParallelOptions { MaxDegreeOfParallelism = allowedParallelism }, (toCompile, state) => { if (!ProcessSourceFile(toCompile.Item1, toCompile.Item2)) { state.Stop(); } }); if (!result.IsCompleted && !Log.HasLoggedErrors) { Log.LogError("Unknown failure occured while compiling. Check logs to get more details."); } if (!Log.HasLoggedErrors) { int numUnchanged = _totalFiles - _numCompiled; if (numUnchanged > 0) { Log.LogMessage(MessageImportance.High, $"[{numUnchanged}/{_totalFiles}] unchanged."); } } } finally { if (!string.IsNullOrEmpty(_tempPath)) { Directory.Delete(_tempPath, true); } } OutputFiles = outputItems.ToArray(); return(!Log.HasLoggedErrors); bool ProcessSourceFile(string srcFile, string objFile) { string tmpObjFile = Path.GetTempFileName(); try { string command = $"emcc {Arguments} -c -o \"{tmpObjFile}\" \"{srcFile}\""; var startTime = DateTime.Now; // Log the command in a compact format which can be copy pasted StringBuilder envStr = new StringBuilder(string.Empty); foreach (var key in envVarsDict.Keys) { envStr.Append($"{key}={envVarsDict[key]} "); } Log.LogMessage(MessageImportance.Low, $"Exec: {envStr}{command}"); (int exitCode, string output) = Utils.RunShellCommand( Log, command, envVarsDict, workingDir: Environment.CurrentDirectory, logStdErrAsMessage: true, debugMessageImportance: messageImportance, label: Path.GetFileName(srcFile)); var endTime = DateTime.Now; var elapsedSecs = (endTime - startTime).TotalSeconds; if (exitCode != 0) { Log.LogError($"Failed to compile {srcFile} -> {objFile}{Environment.NewLine}{output} [took {elapsedSecs:F}s]"); return(false); } if (!Utils.CopyIfDifferent(tmpObjFile, objFile, useHash: true)) { Log.LogMessage(MessageImportance.Low, $"Did not overwrite {objFile} as the contents are unchanged"); } else { Log.LogMessage(MessageImportance.Low, $"Copied {tmpObjFile} to {objFile}"); } outputItems.Add(CreateOutputItemFor(srcFile, objFile)); int count = Interlocked.Increment(ref _numCompiled); Log.LogMessage(MessageImportance.High, $"[{count}/{_totalFiles}] {Path.GetFileName(srcFile)} -> {Path.GetFileName(objFile)} [took {elapsedSecs:F}s]"); return(!Log.HasLoggedErrors); } catch (Exception ex) { Log.LogError($"Failed to compile {srcFile} -> {objFile}{Environment.NewLine}{ex.Message}"); return(false); } finally { File.Delete(tmpObjFile); } } ITaskItem CreateOutputItemFor(string srcFile, string objFile) { ITaskItem newItem = new TaskItem(objFile); newItem.SetMetadata("SourceFile", srcFile); return(newItem); } }
public static void ParallelForEach() { string[] data = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve" }; ParallelLoopResult result = Parallel.ForEach <string>(data, s => { WriteLine(s); }); }
/// <summary> /// For(Int32, Int32, ParallelOptions, Action<Int32>) /// </summary> public void ParallelForExample4() { CancellationTokenSource cancellationSource = new CancellationTokenSource(); ParallelOptions options = new ParallelOptions(); options.CancellationToken = cancellationSource.Token; //Parallel.For(0, 10, options, i => //{ // // Simulate a cancellation of the loop when i=2 // if (i == 2) // { // cancellationSource.Cancel(); // } //}); try { ParallelLoopResult loopResult = Parallel.For( 0, 10, options, (i, loopState) => { Debug.WriteLine("Start Thread={0}, i={1}", Thread.CurrentThread.ManagedThreadId, i); // Simulate a cancellation of the loop when i=2 if (i == 2) { cancellationSource.Cancel(); } // Simulates a long execution for (int j = 0; j < 10; j++) { Thread.Sleep(1 * 200); // check to see whether or not to continue if (loopState.ShouldExitCurrentIteration) { return; } } Debug.WriteLine("Finish Thread={0}, i={1}", Thread.CurrentThread.ManagedThreadId, i); } ); if (loopResult.IsCompleted) { Debug.WriteLine("All iterations completed successfully. THIS WAS NOT EXPECTED."); } } // No exception is expected in this example, but if one is still thrown from a task, // it will be wrapped in AggregateException and propagated to the main thread. catch (AggregateException e) { Debug.WriteLine("Parallel.For has thrown an AggregateException. THIS WAS NOT EXPECTED.\n{0}", e); } // Catching the cancellation exception catch (OperationCanceledException e) { Debug.WriteLine("An iteration has triggered a cancellation. THIS WAS EXPECTED.\n{0}", e.ToString()); } finally { cancellationSource.Dispose(); } }
/// <inheritdoc /> public override async Task RunAsync(RuleContext context) { Block block = context.ValidationContext.BlockToValidate; ChainedHeader index = context.ValidationContext.ChainedHeaderToValidate; DeploymentFlags flags = context.Flags; UnspentOutputSet view = (context as UtxoRuleContext).UnspentOutputSet; long sigOpsCost = 0; Money fees = Money.Zero; var inputsToCheck = new List <(Transaction tx, int inputIndexCopy, TxOut txOut, PrecomputedTransactionData txData, TxIn input, DeploymentFlags flags)>(); for (int txIndex = 0; txIndex < block.Transactions.Count; txIndex++) { Transaction tx = block.Transactions[txIndex]; if (!context.SkipValidation) { if (!tx.IsCoinBase && !view.HaveInputs(tx)) { this.Logger.LogDebug("Transaction '{0}' has not inputs", tx.GetHash()); this.Logger.LogTrace("(-)[BAD_TX_NO_INPUT]"); ConsensusErrors.BadTransactionMissingInput.Throw(); } if (!this.IsTxFinal(tx, context)) { this.Logger.LogDebug("Transaction '{0}' is not final", tx.GetHash()); this.Logger.LogTrace("(-)[BAD_TX_NON_FINAL]"); ConsensusErrors.BadTransactionNonFinal.Throw(); } // GetTransactionSignatureOperationCost counts 3 types of sigops: // * legacy (always), // * p2sh (when P2SH enabled in flags and excludes coinbase), // * witness (when witness enabled in flags and excludes coinbase). sigOpsCost += this.GetTransactionSignatureOperationCost(tx, view, flags); if (sigOpsCost > this.ConsensusOptions.MaxBlockSigopsCost) { this.Logger.LogTrace("(-)[BAD_BLOCK_SIG_OPS]"); ConsensusErrors.BadBlockSigOps.Throw(); } if (!tx.IsCoinBase) { this.CheckInputs(tx, view, index.Height); fees += this.GetTransactionFee(view, tx); var txData = new PrecomputedTransactionData(tx); for (int inputIndex = 0; inputIndex < tx.Inputs.Count; inputIndex++) { TxIn input = tx.Inputs[inputIndex]; inputsToCheck.Add(( tx: tx, inputIndexCopy: inputIndex, txOut: view.GetOutputFor(input), txData, input: input, flags )); } } } this.UpdateCoinView(context, tx); } if (!context.SkipValidation) { this.CheckBlockReward(context, fees, index.Height, block); // Start the Parallel loop on a thread so its result can be awaited rather than blocking Task <ParallelLoopResult> checkInputsInParallel = Task.Run(() => { return(Parallel.ForEach(inputsToCheck, (input, state) => { if (state.ShouldExitCurrentIteration) { return; } if (!this.CheckInput(input.tx, input.inputIndexCopy, input.txOut, input.txData, input.input, input.flags)) { state.Stop(); } })); }); ParallelLoopResult loopResult = await checkInputsInParallel.ConfigureAwait(false); if (!loopResult.IsCompleted) { this.Logger.LogTrace("(-)[BAD_TX_SCRIPT]"); ConsensusErrors.BadTransactionScriptError.Throw(); } } else { this.Logger.LogDebug("BIP68, SigOp cost, and block reward validation skipped for block at height {0}.", index.Height); } }
public Color <byte>[,] Median3x3ParallelCancel(Color <byte>[,] imageUnfiltred) { string name = "Median3x3ParallelCancel"; int progress = 2; int height = imageUnfiltred.GetLength(0); int width = imageUnfiltred.GetLength(1); Color <byte>[,] imageFiltred = new Color <byte> [height, width]; ParallelOptions parallelOptions = new ParallelOptions(); CancellationTokenSource cancellationOption = new CancellationTokenSource(); parallelOptions.CancellationToken = cancellationOption.Token; try { ParallelLoopResult loopResult = Parallel.For (1, height - 1, parallelOptions, i => { Console.WriteLine($"{name}: thread #{Thread.CurrentThread.ManagedThreadId.ToString()} on height {i.ToString()} -> applicating"); for (int j = 1; j < width - 1; j++) { if (cancellationOption.IsCancellationRequested) { break; } Color <int[]> newColors = new Color <int[]> { Red = new int[9], Green = new int[9], Blue = new int[9] }; newColors = Convolution(imageUnfiltred, i, j, newColors); Array.Sort(newColors.Red); Array.Sort(newColors.Green); Array.Sort(newColors.Blue); imageFiltred[i, j].Red = (byte)(newColors.Red[4]); imageFiltred[i, j].Green = (byte)(newColors.Green[4]); imageFiltred[i, j].Blue = (byte)(newColors.Blue[4]); } Interlocked.Increment(ref progress); if (!ProgressCallback(progress, height)) { Console.WriteLine($"{name}: thread #{Thread.CurrentThread.ManagedThreadId.ToString()} on height {i.ToString()} -> " + "canceling"); cancellationOption.Cancel(); } } ); if (loopResult.IsCompleted) { Console.WriteLine($"{name} is succesfully applicated"); } } catch (OperationCanceledException cancellation) { Console.WriteLine($"{name} application has been canceled:\n{cancellation.ToString()}"); imageUnfiltred = null; imageFiltred = null; return(imageFiltred); } finally { cancellationOption.Dispose(); } imageUnfiltred = null; return(imageFiltred); }
private void CollectorHostRefreshCurrentState(CollectorHost collectorHost, bool disablePollingOverrides = false) { if (!AbortPolling) { RaiseCollectorHostCalled(collectorHost); try { MonitorState chms = null; collectorHost.RunTimeMasterKey = ""; collectorHost.RunTimeUserNameCacheFile = ""; collectorHost.BlockedCollectorAgentTypes = new List <string>(); collectorHost.BlockedCollectorAgentTypes.AddRange(BlockedCollectorAgentTypes.ToArray()); if (collectorHost.RunAsEnabled && collectorHost.RunAs != null && collectorHost.RunAs.Length > 0) { if (UserNameCacheMasterKey.Length > 0 && System.IO.File.Exists(UserNameCacheFilePath) && QuickMon.Security.CredentialManager.IsAccountPersisted(UserNameCacheFilePath, UserNameCacheMasterKey, collectorHost.RunAs) && QuickMon.Security.CredentialManager.IsAccountDecryptable(UserNameCacheFilePath, UserNameCacheMasterKey, collectorHost.RunAs)) { collectorHost.RunTimeMasterKey = UserNameCacheMasterKey; collectorHost.RunTimeUserNameCacheFile = UserNameCacheFilePath; } else if (ApplicationUserNameCacheMasterKey != null && ApplicationUserNameCacheFilePath != null && ApplicationUserNameCacheMasterKey.Length > 0 && System.IO.File.Exists(ApplicationUserNameCacheFilePath) && QuickMon.Security.CredentialManager.IsAccountPersisted(ApplicationUserNameCacheFilePath, ApplicationUserNameCacheMasterKey, collectorHost.RunAs) && QuickMon.Security.CredentialManager.IsAccountDecryptable(ApplicationUserNameCacheFilePath, ApplicationUserNameCacheMasterKey, collectorHost.RunAs)) { collectorHost.RunTimeMasterKey = ApplicationUserNameCacheMasterKey; collectorHost.RunTimeUserNameCacheFile = ApplicationUserNameCacheFilePath; } } //collectorHost.ApplyConfigVarsNow(ConfigVariables); chms = collectorHost.RefreshCurrentState(disablePollingOverrides); #region Do/Check/Set dependant CollectorHosts if (chms.State == CollectorState.Error && collectorHost.ChildCheckBehaviour != ChildCheckBehaviour.ContinueOnWarningOrError) { SetDependantCollectorHostStates(collectorHost, CollectorState.NotAvailable); } else if (chms.State == CollectorState.Warning && collectorHost.ChildCheckBehaviour == ChildCheckBehaviour.OnlyRunOnSuccess) { SetDependantCollectorHostStates(collectorHost, CollectorState.NotAvailable); } else if (chms.State == CollectorState.Disabled || chms.State == CollectorState.ConfigurationError || !collectorHost.IsEnabledNow()) { SetDependantCollectorHostStates(collectorHost, CollectorState.Disabled); } else { //Remote execute and dependant Collector Hosts if (collectorHost.ForceRemoteExcuteOnChildCollectors) { collectorHost.OverrideForceRemoteExcuteOnChildCollectors = true; SetChildCollectorHostRemoteExecuteDetails(collectorHost, collectorHost.RemoteAgentHostAddress, collectorHost.RemoteAgentHostPort); } //Polling overrides and dependant Collector Hosts if (collectorHost.EnabledPollingOverride) { SetChildCollectorHostPollingOverrides(collectorHost); } #region Loop through dependant CollectorHosts if (ConcurrencyLevel > 1) { ParallelOptions po = new ParallelOptions() { MaxDegreeOfParallelism = ConcurrencyLevel }; ParallelLoopResult parResult = Parallel.ForEach((from c in CollectorHosts where c.ParentCollectorId == collectorHost.UniqueId select c), po, dependentCollectorHost => CollectorHostRefreshCurrentState(dependentCollectorHost, disablePollingOverrides)); if (!parResult.IsCompleted) { SendNotifierAlert(AlertLevel.Error, DetailLevel.All, "Error querying collectors in parralel"); } } else //use old single threaded way { //Refresh states foreach (CollectorHost dependentCollectorHost in (from c in CollectorHosts where c.ParentCollectorId == collectorHost.UniqueId select c)) { CollectorHostRefreshCurrentState(dependentCollectorHost, disablePollingOverrides); } } #endregion } #endregion LoggingCollectorEvent(string.Format("Collector '{0}' return state '{1}'", collectorHost.Name, collectorHost.CurrentState), collectorHost); } catch (Exception ex) { WriteLogging(string.Format("CollectorHostRefreshCurrentState error: {0}", ex.Message)); if (!ex.Message.Contains("Collection was modified; enumeration operation may not execute")) { RaiseMonitorPackError("Internal error. Collector config was modified while in use!"); } else { RaiseCollectorError(collectorHost, ex.Message); } } } }
public List <int> edgeTree = new List <int>(); //边界木 /// <summary> /// 划分空间结构单元 /// </summary> public void DivideUnit(List <Tree> array) { //清理空间结构单元 near4Units.Clear(); allUnits.Clear(); edgeTree.Clear(); //for (int i = 0; i < array.Count; i++) ParallelLoopResult parallelresult = Parallel.For(0, array.Count, i => { List <indivTree> TreeInForest = new List <indivTree>(); List <indivTree> near4trees = new List <indivTree>(); //最近四株木 List <indivTree> ssUnit = new List <indivTree>(); //空间结构单元 for (int j = 0; j < array.Count; j++) { indivTree iTree = new indivTree(); if (i != j) { //id iTree.centerID = i; iTree.id = j; //计算距离 double distance = Math.Sqrt((array[i].X - array[j].X) * (array[i].X - array[j].X) + (array[i].Y - array[j].Y) * (array[i].Y - array[j].Y)); iTree.distance = distance; //确定象限 double x_ = array[j].X - array[i].X; double y_ = array[j].Y - array[i].Y; if (x_ > 0 && y_ >= 0) { iTree.quadrant = 1; } else if (x_ >= 0 && y_ < 0) { iTree.quadrant = 4; } else if (x_ < 0 && y_ <= 0) { iTree.quadrant = 3; } else if (x_ <= 0 && y_ > 0) { iTree.quadrant = 2; } else { Console.WriteLine("Abnormality of tree position... Tree ID: " + i); return; } TreeInForest.Add(iTree); } } //对树木按距离进行排序 TreeInForest.Sort((left, right) => left.distance.CompareTo(right.distance)); //{ // if (left.distance > right.distance) // return 1; // else if (left.distance == right.distance) // return 0; // else // return -1; //}); //最近四株木 for (int j = 0; j < 4; j++) { near4trees.Add(TreeInForest[j]); } //全象限空间结构单元 int lackQuad; ssUnit = near4trees; if (!quadContained(1, ssUnit)) { int treeCount; lackQuad = 1; for (treeCount = 4; treeCount < TreeInForest.Count; treeCount++) { if (TreeInForest[treeCount].quadrant == lackQuad) { ssUnit.Add(TreeInForest[treeCount]); break; } } if (treeCount >= TreeInForest.Count) { edgeTree.Add(i); } } if (!quadContained(2, ssUnit)) { int treeCount; lackQuad = 2; for (treeCount = 4; treeCount < TreeInForest.Count; treeCount++) { if (TreeInForest[treeCount].quadrant == lackQuad) { ssUnit.Add(TreeInForest[treeCount]); break; } } if (treeCount >= TreeInForest.Count) { edgeTree.Add(i); } } if (!quadContained(3, ssUnit)) { int treeCount; lackQuad = 3; for (treeCount = 4; treeCount < TreeInForest.Count; treeCount++) { if (TreeInForest[treeCount].quadrant == lackQuad) { ssUnit.Add(TreeInForest[treeCount]); break; } } if (treeCount >= TreeInForest.Count) { edgeTree.Add(i); } } if (!quadContained(4, ssUnit)) { int treeCount; lackQuad = 4; for (treeCount = 4; treeCount < TreeInForest.Count; treeCount++) { if (TreeInForest[treeCount].quadrant == lackQuad) { ssUnit.Add(TreeInForest[treeCount]); break; } } if (treeCount >= TreeInForest.Count) { edgeTree.Add(i); } } //保存所有惠氏空间结构单元 unit_tree ut; ut.centerID = i; ut.id = new List <int>(); ut.quad = new List <int>(); for (int j = 0; j < 4; j++) { ut.id.Add(ssUnit[j].id); ut.quad.Add(ssUnit[j].quadrant); } lock (near4Units) { near4Units.Add(ut); } //保存所有全象限空间结构单元 ut.id.Clear(); ut.quad.Clear(); for (int j = 0; j < ssUnit.Count; j++) { ut.id.Add(ssUnit[j].id); ut.quad.Add(ssUnit[j].quadrant); } lock (allUnits) { allUnits.Add(ut); } }); }
public int Compress(VoidPtr srcAddr, int srcLen, Stream outStream, IProgressTracker progress, bool YAZ0) { _pSrc = (byte *)srcAddr; _sourceLen = srcLen; int chunkCount = (int)Math.Ceiling((double)srcLen / _threadChunk); if (progress != null) { progress.Begin(0, srcLen, 0); } _contractions = new List <Contraction> [chunkCount]; if (YAZ0) { outStream.WriteByte(0x59); outStream.WriteByte(0x61); outStream.WriteByte(0x7A); outStream.WriteByte(0x30); outStream.WriteByte((byte)((_sourceLen >> 24) & 0xFF)); outStream.WriteByte((byte)((_sourceLen >> 16) & 0xFF)); outStream.WriteByte((byte)((_sourceLen >> 08) & 0xFF)); outStream.WriteByte((byte)((_sourceLen >> 00) & 0xFF)); for (int i = 0; i < 8; i++) { outStream.WriteByte(0); } } else { CompressionHeader header = new CompressionHeader(); header.Algorithm = CompressionType.RunLength; header.ExpandedSize = (int)_sourceLen; outStream.Write(&header, 4 + (header.LargeSize ? 4 : 0)); } ParallelLoopResult result = Parallel.For(0, chunkCount, FindContractions); while (!result.IsCompleted) { Thread.Sleep(100); } List <Contraction> fullContractions; int codeBits, tempLoc, current; byte codeByte; byte[] temp; int lastUpdate = srcLen; fullContractions = new List <Contraction>(); for (int i = 0; i < _contractions.Length; i++) { fullContractions.AddRange(_contractions[i]); _contractions[i].Clear(); _contractions[i] = null; } _contractions = null; temp = new byte[3 * 8]; codeBits = 0; codeByte = 0; tempLoc = 0; current = 0; for (int i = 0; i < srcLen;) { if (codeBits == 8) { outStream.WriteByte(codeByte); outStream.Write(temp, 0, tempLoc); codeBits = 0; codeByte = 0; tempLoc = 0; } if (current < fullContractions.Count && fullContractions[current].Location == i) { if (fullContractions[current].Size >= 0x12) { temp[tempLoc++] = (byte)(fullContractions[current].Offset >> 8); temp[tempLoc++] = (byte)(fullContractions[current].Offset & 0xFF); temp[tempLoc++] = (byte)(fullContractions[current].Size - 0x12); } else { temp[tempLoc++] = (byte)((fullContractions[current].Offset >> 8) | ((fullContractions[current].Size - 2) << 4)); temp[tempLoc++] = (byte)(fullContractions[current].Offset & 0xFF); } i += fullContractions[current++].Size; while (current < fullContractions.Count && fullContractions[current].Location < i) { current++; } } else { codeByte |= (byte)(1 << (7 - codeBits)); temp[tempLoc++] = _pSrc[i++]; } codeBits++; if (progress != null) { if (i % 0x4000 == 0) { progress.Update(i); } } } outStream.WriteByte(codeByte); outStream.Write(temp, 0, tempLoc); outStream.Flush(); if (progress != null) { progress.Finish(); } return((int)outStream.Length); }
public void DoDownload() { jsonFile = Path.Combine(DownDir, "meta.json"); if (!File.Exists(jsonFile)) { return; } string jsonContent = File.ReadAllText(jsonFile); JObject initJson = JObject.Parse(jsonContent); JArray parts = JArray.Parse(initJson["m3u8Info"]["segments"].ToString()); //大分组 string segCount = initJson["m3u8Info"]["count"].ToString(); string oriCount = initJson["m3u8Info"]["originalCount"].ToString(); //原始分片数量 string isVOD = initJson["m3u8Info"]["vod"].ToString(); try { if (initJson["m3u8Info"]["audio"].ToString() != "") { externalAudio = true; } externalAudioUrl = initJson["m3u8Info"]["audio"].ToString(); LOGGER.WriteLine(strings.hasExternalAudioTrack); LOGGER.PrintLine(strings.hasExternalAudioTrack, LOGGER.Warning); } catch (Exception) {} try { if (initJson["m3u8Info"]["sub"].ToString() != "") { externalSub = true; } externalSubUrl = initJson["m3u8Info"]["sub"].ToString(); LOGGER.WriteLine(strings.hasExternalSubtitleTrack); LOGGER.PrintLine(strings.hasExternalSubtitleTrack, LOGGER.Warning); } catch (Exception) { } total = Convert.ToInt32(segCount); PartsCount = parts.Count; segsPadZero = string.Empty.PadRight(oriCount.Length, '0'); partsPadZero = string.Empty.PadRight(Convert.ToString(parts.Count).Length, '0'); //是直播视频 if (isVOD == "False") { return; } Global.ShouldStop = false; //是否该停止下载 if (!Directory.Exists(DownDir)) { Directory.CreateDirectory(DownDir); //新建文件夹 } Watcher watcher = new Watcher(DownDir); watcher.Total = total; watcher.PartsCount = PartsCount; watcher.WatcherStrat(); //开始计算速度 timer.Enabled = true; cts = new CancellationTokenSource(); //开始调用下载 LOGGER.WriteLine(strings.startDownloading); LOGGER.PrintLine(strings.startDownloading, LOGGER.Warning); //下载MAP文件(若有) try { Downloader sd = new Downloader(); sd.TimeOut = TimeOut; sd.FileUrl = initJson["m3u8Info"]["extMAP"].Value <string>(); sd.Headers = Headers; sd.Method = "NONE"; if (sd.FileUrl.Contains("|")) //有range { string[] tmp = sd.FileUrl.Split('|'); sd.FileUrl = tmp[0]; sd.StartByte = Convert.ToUInt32(tmp[1].Split('@')[1]); sd.ExpectByte = Convert.ToUInt32(tmp[1].Split('@')[0]); } sd.SavePath = DownDir + "\\!MAP.tsdownloading"; if (File.Exists(sd.SavePath)) { File.Delete(sd.SavePath); } if (File.Exists(DownDir + "\\Part_0\\!MAP.ts")) { File.Delete(DownDir + "\\Part_0\\!MAP.ts"); } LOGGER.PrintLine(strings.downloadingMapFile); sd.Down(); //开始下载 } catch (Exception e) { //LOG.WriteLineError(e.ToString()); } //首先下载第一个分片 JToken firstSeg = JArray.Parse(parts[0].ToString())[0]; if (!File.Exists(DownDir + "\\Part_" + 0.ToString(partsPadZero) + "\\" + firstSeg["index"].Value <int>().ToString(segsPadZero) + ".ts")) { try { Downloader sd = new Downloader(); sd.TimeOut = TimeOut; sd.SegDur = firstSeg["duration"].Value <double>(); if (sd.SegDur < 0) { sd.SegDur = 0; //防止负数 } sd.FileUrl = firstSeg["segUri"].Value <string>(); //VTT字幕 if (isVTT == false && (sd.FileUrl.Trim('\"').EndsWith(".vtt") || sd.FileUrl.Trim('\"').EndsWith(".webvtt"))) { isVTT = true; } sd.Method = firstSeg["method"].Value <string>(); if (sd.Method != "NONE") { sd.Key = firstSeg["key"].Value <string>(); sd.Iv = firstSeg["iv"].Value <string>(); } if (firstSeg["expectByte"] != null) { sd.ExpectByte = firstSeg["expectByte"].Value <long>(); } if (firstSeg["startByte"] != null) { sd.StartByte = firstSeg["startByte"].Value <long>(); } sd.Headers = Headers; sd.SavePath = DownDir + "\\Part_" + 0.ToString(partsPadZero) + "\\" + firstSeg["index"].Value <int>().ToString(segsPadZero) + ".tsdownloading"; if (File.Exists(sd.SavePath)) { File.Delete(sd.SavePath); } LOGGER.PrintLine(strings.downloadingFirstSegement); if (!Global.ShouldStop) { sd.Down(); //开始下载 } } catch (Exception e) { //LOG.WriteLineError(e.ToString()); } } if (Global.HadReadInfo == false) { string href = DownDir + "\\Part_" + 0.ToString(partsPadZero) + "\\" + firstSeg["index"].Value <int>().ToString(segsPadZero) + ".ts"; if (File.Exists(DownDir + "\\!MAP.ts")) { href = DownDir + "\\!MAP.ts"; } Global.GzipHandler(href); bool flag = false; foreach (string ss in (string[])Global.GetVideoInfo(href).ToArray(typeof(string))) { LOGGER.WriteLine(ss.Trim()); LOGGER.PrintLine(ss.Trim(), 0); if (ss.Trim().Contains("Error in reading file")) { flag = true; } } LOGGER.PrintLine(strings.waitForCompletion, LOGGER.Warning); if (!flag) { Global.HadReadInfo = true; } } //多线程设置 ParallelOptions parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Threads, CancellationToken = cts.Token }; //构造包含所有分片的新的segments JArray segments = new JArray(); for (int i = 0; i < parts.Count; i++) { var tmp = JArray.Parse(parts[i].ToString()); for (int j = 0; j < tmp.Count; j++) { JObject t = (JObject)tmp[j]; t.Add("part", i); segments.Add(t); } } //剔除第一个分片(已下载过) segments.RemoveAt(0); try { ParallelLoopResult result = Parallel.ForEach(segments, parallelOptions, () => new Downloader(), (info, loopstate, index, sd) => { if (Global.ShouldStop) { loopstate.Stop(); } else { sd.TimeOut = TimeOut; sd.SegDur = info["duration"].Value <double>(); if (sd.SegDur < 0) { sd.SegDur = 0; //防止负数 } sd.FileUrl = info["segUri"].Value <string>(); //VTT字幕 if (isVTT == false && (sd.FileUrl.Trim('\"').EndsWith(".vtt") || sd.FileUrl.Trim('\"').EndsWith(".webvtt"))) { isVTT = true; } sd.Method = info["method"].Value <string>(); if (sd.Method != "NONE") { sd.Key = info["key"].Value <string>(); sd.Iv = info["iv"].Value <string>(); } if (firstSeg["expectByte"] != null) { sd.ExpectByte = info["expectByte"].Value <long>(); } if (firstSeg["startByte"] != null) { sd.StartByte = info["startByte"].Value <long>(); } sd.Headers = Headers; sd.SavePath = DownDir + "\\Part_" + info["part"].Value <int>().ToString(partsPadZero) + "\\" + info["index"].Value <int>().ToString(segsPadZero) + ".tsdownloading"; if (File.Exists(sd.SavePath)) { File.Delete(sd.SavePath); } if (!Global.ShouldStop) { sd.Down(); //开始下载 } } return(sd); }, (sd) => { }); if (result.IsCompleted) { //LOGGER.WriteLine("Part " + (info["part"].Value<int>() + 1).ToString(partsPadZero) + " of " + parts.Count + " Completed"); } } catch (Exception) { ;//捕获取消循环产生的异常 } finally { cts.Dispose(); } watcher.WatcherStop(); //停止速度监测 timer.Enabled = false; //检测是否下完 IsComplete(Convert.ToInt32(segCount)); }
static void Main(string[] args) { Console.WriteLine("Задание 1"); Action action1 = new Action(EasyNumbersIrato); //delegate Инкапсулирует метод, который не имеет параметров и не возвращает значений Stopwatch watch = Stopwatch.StartNew(); //Предоставляет набор методов и свойств, которые можно использовать для точного измерения затраченного времени. StartNew Создает и запускает задачу task1 = Task.Factory.StartNew(action1); //StartNew Создает и запускает задачу task1.Wait(); //Ожидает завершения выполнения задачи task1.Dispose(); //Реализация метода Dispose в основном используется для освобождения неуправляемых ресурсов. watch.Stop(); //вызов Stop завершает текущую меру интервала и замораживает совокупное значение затраченного времени Console.WriteLine("Time for task: " + watch.Elapsed.ToString()); //событие, которое Происходит по истечении интервала времени Console.WriteLine("Task Completed: " + task1.IsCompleted.ToString()); //Получает значение, указывающее, завершена ли задача Console.WriteLine("Status: " + task1.Status.ToString()); //Получает состояние данной задачи Console.WriteLine(); Console.WriteLine("Задание 2"); CancellationTokenSource cancellation = new CancellationTokenSource(); //создаем токен-ресурс, то есть отмену выполнения задачи task1 = Task.Factory.StartNew(action1, cancellation.Token); ////StartNew Создает и запускает задачу и используем токен-ресурс cancellation.Cancel(); //Возвращает или задает значение, показывающее, следует ли отменить событие Console.WriteLine("Task Canceled: " + task1.IsCanceled.ToString()); //Возвращает значение, указывающее, завершилось ли выполнение данного экземпляра Task из-за отмены Console.WriteLine("Status: " + task1.Status.ToString()); //Получает состояние данной задачи Console.WriteLine(); Console.WriteLine("Задание 3 - 4"); Task <int> fact1 = new Task <int>(() => Factorial(5)); //Task представляет собой работу над потоками для выполнения асинхронных операций Task <int> fact2 = fact1.ContinueWith(a => Factorial(10)); //Создает продолжение, которое выполняется асинхронно после завершения выполнения целевой задачи Task <int> fact3 = fact2.ContinueWith(a => Factorial(15)); Task <int> res = new Task <int>(() => Sum(fact1.Result, fact2.Result, fact3.Result)); fact1.Start(); //Запускает ресурс процесса и связывает его с компонентом Process res.Start(); //Запускает ресурс процесса и связывает его с компонентом Process var awaiter = res.GetAwaiter(); //Получает объект типа awaiter, используемый для данного объекта Task awaiter.OnCompleted(() => Console.WriteLine("Last Result:" + awaiter.GetResult())); //Уведомляет наблюдателя о том, что поставщик завершил отправку push-уведомлений. GetResult Завершает ожидание завершения асинхронной задачи Thread.Sleep(1000); Console.WriteLine(); Console.WriteLine("Задание 5"); watch = Stopwatch.StartNew();//StartNew Создает и запускает задачу int[] array = new int[100000000]; ParallelLoopResult result = Parallel.For(0, 10000000, (int z, ParallelLoopState loop) => { array[z] = z + 1; if (z == 1000) { loop.Break(); } }); //ParallelLoopResult Предоставляет состояние выполнения цикла Parallel. Parallel.For - Выполняет цикл for, в котором итерации могут выполняться параллельно if (result.IsCompleted) { Console.WriteLine("Выполнен"); ////Получает значение, указывающее, завершена ли задача } else { Console.WriteLine("Выполнение цикла завершено на итерации {0}", result.LowestBreakIteration.ToString()); //Получает первую итерацию цикла, из которой был прервал метод } watch.Stop(); //вызов Stop завершает текущую меру интервала и замораживает совокупное значение затраченного времени Console.WriteLine("Time for task: " + watch.Elapsed.ToString()); ////событие, которое Происходит по истечении интервала времени Console.WriteLine(); Console.WriteLine("Задание 6"); Parallel.Invoke(() =>//Выполняет все предоставленные действия, по возможности в параллельном режиме { for (int i = 0; i < 10; i++) { Console.Write("1 "); } }, () => { for (int i = 0; i < 10; i++) { Console.Write("2 "); } }); Console.WriteLine(); Console.WriteLine("\nЗадание 7"); bc = new BlockingCollection <int>(5);// Создадим задачи поставщика и потребителя Task Pr = new Task(producer); Task Cn = new Task(consumer); Pr.Start(); //Запускает ресурс процесса и связывает его с компонентом Process Cn.Start(); //Запускает ресурс процесса и связывает его с компонентом Process try { Task.WaitAll(Cn, Pr);//Ожидает завершения выполнения всех указанных объектов Task } catch (Exception ex) { Console.WriteLine(ex); } finally { Cn.Dispose();////Реализация метода Dispose в основном используется для освобождения неуправляемых ресурсов Pr.Dispose(); bc.Dispose(); } Console.WriteLine("\nЗадание 8"); SayAsync(); Console.WriteLine("I\'m first!"); Console.ReadKey(); }
static void Main(string[] args) { vehicleCollection = new List <Vehicle>(); Aircraft craft1 = new Aircraft("MIG-21", 15.6); Amphibian craft2 = new Amphibian("Amphibian-1", 30); Aircraft craft3 = new Aircraft("SU-27", 23.2); Amphibian craft4 = new Amphibian("Amphibian-2", 33.3); vehicleCollection.Add(craft1); vehicleCollection.Add(craft2); vehicleCollection.Add(craft3); vehicleCollection.Add(craft4); int choice = defaultUserChoice; do { Console.WriteLine("**************************************"); Console.WriteLine("* Главное меню *"); Console.WriteLine("* выберите действие *"); Console.WriteLine("**************************************"); Console.WriteLine("1 - просмотр коллекции"); Console.WriteLine("2 - добавление элемента (конструктор с двумя параметрами)"); Console.WriteLine("3 - добавление элемента по указанному индексу"); Console.WriteLine("4 - нахождение элемента с начала коллекции"); Console.WriteLine("5 - нахождение элемента с конца коллекции"); Console.WriteLine("6 - удаление элемента по индексу"); Console.WriteLine("7 - удаление элемента по значению"); Console.WriteLine("8 - реверс коллекции"); Console.WriteLine("9 - сортировка"); Console.WriteLine("10 - выполнение методов всех обьектов, поддерживающих Interface2"); Console.WriteLine("11 - при помощи Parallel.ForEach итерация по коллекции"); Console.WriteLine("0 - выход"); choice = int.Parse(Console.ReadLine()); switch (choice) { case 1: ShowAll(); break; case 2: AddNewObject(); break; case 3: AddByIndex(); break; case 4: FirstElement(); break; case 5: LastElement(); break; case 6: DellByIndex(); break; case 7: DellByValue(); break; case 8: ReverseCollection(); break; case 9: SortCollection(); break; case 10: ISwiminigMethod(); break; case 11: ParallelLoopResult result = Parallel.ForEach(vehicleCollection, item => { Console.WriteLine(item.name); }); break; default: return; } }while (choice != 0); }
static void Main(string[] args) { Console.WriteLine("Первое задание:"); Stopwatch stopwatch = new Stopwatch(); // создание обекта для замера времени работы Task algorithmTask = new Task(() => Algorithm(2500, 24354)); // создание новой задачи из метода stopwatch.Start(); // начало отсчета времени выполнения algorithmTask.Start(); // старт задачи Thread.Sleep(500); Console.WriteLine("Статус задачи: " + algorithmTask.Status); // вывод состояния задачи Console.WriteLine("Завершена ли задача: " + algorithmTask.IsCompleted); // вывод состояния задачи algorithmTask.Wait(); //ожидание завершения задачи stopwatch.Stop(); //остановка счетчика TimeSpan timeSpan = stopwatch.Elapsed; //время за которое выполнилась задача Console.WriteLine("Время выполнения: " + timeSpan); //вывод затраченого времени в консоль Console.ReadKey(); Console.WriteLine("\nВторое задание:"); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); // создание токена отмены(позволяет принудительно завершать задачи при наличии проверки флага отмены в теле самой задачи) CancellationToken cancellationToken = cancellationTokenSource.Token; Task algorithmTask2 = new Task(() => Algorithm2(2500, 24354, cancellationToken)); //создание задачи и передача токена отмены algorithmTask2.Start(); // старт задачи Thread.Sleep(2500); cancellationTokenSource.Cancel(); //завершение задачи через токен отмены Console.ReadKey(); int z = 0; Func <int> func = () => ++ z; Console.WriteLine("\nТретье задание:"); //создание трех задач Task <int> returnOne = new Task <int>(func); Task <int> returnTwo = new Task <int>(func); Task <int> returnThree = new Task <int>(func); //старт этих задач returnOne.Start(); returnTwo.Start(); returnThree.Start(); int Factorial() => returnOne.Result *returnTwo.Result *returnThree.Result; //создание локальной функции которая использует результаты задач Task <int> resultTask = new Task <int>(Factorial); //создание задачи на основе локальной вункции resultTask.Start(); //старт задачи Console.WriteLine("Result = " + resultTask.Result); //вывод результата Console.ReadKey(); Console.WriteLine("\nЧетвертое задание(1): "); Task taskContOne = new Task(() => //создание основной задачи { Console.WriteLine("Id задачи: {0}", Task.CurrentId); }); Task taskContTwo = taskContOne.ContinueWith(Display); //установка метода или задачи продолжения, он будет вызван как только завершится основная задача taskContOne.Start(); Console.ReadKey(); //тоже самое но ручками Console.WriteLine("\nЧетвертое задание(2): "); Random rand = new Random(); Task <int> what = Task.Run(() => rand.Next(10) * rand.Next(10)); //создаем и сразу же запускаем задачу TaskAwaiter <int> awaiter = what.GetAwaiter(); // получение объекта ожидания задачи awaiter.OnCompleted(() => //назначение метода на событие завершения задачи { Console.WriteLine("Result: " + awaiter.GetResult()); //получение результата выполнения задачи и его вывод на консоль }); Console.ReadKey(); Console.WriteLine("\nПятое задание:"); stopwatch.Restart(); // сброс и перезапуск счетчика времени выполнения Parallel.For(0, 5, CreateBigArr); // создание параллельного цикла от 0 до 5 с установкой метода который будет выполнятся stopwatch.Stop(); Console.WriteLine("Время при Parallel.For: " + stopwatch.Elapsed + '\n'); //вывод затраченного времени stopwatch.Restart(); //перезапуск счетчика времени выполнения for (int j = 0; j < 5; j++) // та же работа но уже в синхронном варианте { int[] arr = new int[1000000]; for (int i = 0; i < arr.Length; i++) { arr[i] = rand.Next(10); } Console.WriteLine("Выполнена задача номер " + j); } stopwatch.Stop(); Console.WriteLine("Время при For: " + stopwatch.Elapsed + '\n'); // вывод времени затраченного на синхронное выполнение Console.WriteLine(); List <int> list = new List <int>() { 1, 2, 3, 4, 5 }; stopwatch.Restart(); //перезапуск счетчика времени выполнения ParallelLoopResult result = Parallel.ForEach <int>(list, CreateBigArr); // теперь параллельный foreach stopwatch.Stop(); Console.WriteLine("Время при Parallel.Foreach: " + stopwatch.Elapsed + '\n'); stopwatch.Restart(); foreach (int x in list)// а темерь обычный foreach { int[] arr = new int[1000000]; for (int i = 0; i < arr.Length; i++) { arr[i] = rand.Next(10); } Console.WriteLine("Выполнена задача номер " + x); } stopwatch.Stop(); Console.WriteLine("Время при Foreach: " + stopwatch.Elapsed + '\n'); Console.ReadKey(); Console.WriteLine("\nШестое задание:"); Parallel.Invoke(() => Algorithm(20, 255), () => Display(algorithmTask)); // паралельный запуск двух методов Console.ReadKey(); Console.WriteLine("\nСедимое задание:"); //не всегда может завершится нормально, коментим и показываем отдельно BlockingCollection <string> bc = new BlockingCollection <string>(5); //создание объекта коллекции Provider p1 = new Provider("Мыло", 10); //создание элементов коллекции которые мы будем добавлять Provider p2 = new Provider("Скраб", 300); Provider p3 = new Provider("Шампунь", 500); Provider p4 = new Provider("Гель", 700); Provider p5 = new Provider("Мочалка", 900); Customer c1 = new Customer(); Customer c2 = new Customer(); Customer c3 = new Customer(); Customer c4 = new Customer(); Customer c5 = new Customer(); Customer c6 = new Customer(); Customer c7 = new Customer(); Customer c8 = new Customer(); Customer c9 = new Customer(); Customer c10 = new Customer(); Customer[] carr = { c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 }; // массив покупетелей Task bcTask = Task.Run(async() => // старт задачи с циклом в котором добавляются и покупаются товары пока склад не заполнится { while (bc.Count != bc.BoundedCapacity) { Parallel.Invoke( //параллельный запуск методов добавления и удаления из коллекции () => p1.AddToStore(bc), () => p2.AddToStore(bc), () => p3.AddToStore(bc), () => p4.AddToStore(bc), () => p5.AddToStore(bc), () => c1.TakeFromStore(bc), () => c2.TakeFromStore(bc), () => c3.TakeFromStore(bc), () => c4.TakeFromStore(bc), () => c5.TakeFromStore(bc), () => c6.TakeFromStore(bc), () => c7.TakeFromStore(bc), () => c8.TakeFromStore(bc), () => c9.TakeFromStore(bc), () => c10.TakeFromStore(bc) ); } Console.WriteLine("Склад полон"); }); Console.Write("\n\n"); Console.ReadKey(); Console.WriteLine("\nВосьмое задание:"); AsyncMethod(); // запуск асинхронного метода for (int i = 11; i < 21; i++) //синхронный цикл { Console.WriteLine(i); Thread.Sleep(500); } Console.ReadKey(); }
public Color <byte>[,] Median3x3ParallelLoopState(Color <byte>[,] imageUnfiltred) { string name = "Median3x3ParallelLoopState"; int progress = 2; int height = imageUnfiltred.GetLength(0); int width = imageUnfiltred.GetLength(1); Color <byte>[,] imageFiltred = new Color <byte> [height, width]; ParallelLoopResult loopResult = Parallel.For (1, height - 1, (i, loopState) => { Console.WriteLine($"{name}: thread #{Thread.CurrentThread.ManagedThreadId.ToString()} on height {i.ToString()} -> applicating"); for (int j = 1; j < width - 1; j++) { if (loopState.LowestBreakIteration.HasValue) { break; } Color <int[]> newColors = new Color <int[]> { Red = new int[9], Green = new int[9], Blue = new int[9] }; newColors = Convolution(imageUnfiltred, i, j, newColors); Array.Sort(newColors.Red); Array.Sort(newColors.Green); Array.Sort(newColors.Blue); imageFiltred[i, j].Red = (byte)(newColors.Red[4]); imageFiltred[i, j].Green = (byte)(newColors.Green[4]); imageFiltred[i, j].Blue = (byte)(newColors.Blue[4]); } Interlocked.Increment(ref progress); if (!ProgressCallback(progress, height)) { Console.WriteLine($"{name}: thread #{Thread.CurrentThread.ManagedThreadId.ToString()} on height {i.ToString()} -> " + "canceling"); loopState.Break(); } } ); if (loopResult.IsCompleted) { Console.WriteLine($"{name} is succesfully applicated"); } else { Console.WriteLine($"{name} application has been canceled"); imageUnfiltred = null; imageFiltred = null; return(imageFiltred); } imageUnfiltred = null; return(imageFiltred); }
public CollectorState RefreshStates(bool disablePollingOverrides = false) { AbortPolling = false; IsBusyPolling = true; CollectorState globalState = CollectorState.Good; ResetAllOverrides(); Stopwatch sw = new Stopwatch(); sw.Start(); //First get collectors with no dependancies List <CollectorHost> rootCollectorHosts = (from c in CollectorHosts where c.ParentCollectorId.Length == 0 select c).ToList(); if (ConcurrencyLevel > 1) { ParallelOptions po = new ParallelOptions() { MaxDegreeOfParallelism = ConcurrencyLevel }; ParallelLoopResult parResult = Parallel.ForEach(rootCollectorHosts, po, collectorHost => CollectorHostRefreshCurrentState(collectorHost, disablePollingOverrides)); if (!parResult.IsCompleted) { SendNotifierAlert(AlertLevel.Error, DetailLevel.All, "Error querying collectors in parralel"); } } else //use old single threaded way { //Refresh states foreach (CollectorHost collectorHost in rootCollectorHosts) { CollectorHostRefreshCurrentState(collectorHost, disablePollingOverrides); } } sw.Stop(); PCSetCollectorsQueryTime(sw.ElapsedMilliseconds); LastRefreshDurationMS = sw.ElapsedMilliseconds; #if DEBUG Trace.WriteLine(string.Format("RefreshStates - Global time: {0}ms", sw.ElapsedMilliseconds)); #endif #region Get Global state //All disabled if (CollectorHosts.Count == CollectorHosts.Count(c => c.CurrentState.State == CollectorState.Disabled || c.CurrentState.State == CollectorState.NotInServiceWindow)) { globalState = CollectorState.Disabled; } //All NotAvailable else if (CollectorHosts.Count == CollectorHosts.Count(c => c.CurrentState.State == CollectorState.NotAvailable)) { globalState = CollectorState.NotAvailable; } //All good else if (CollectorHosts.Count == CollectorHosts.Count(c => c.CurrentState.State == CollectorState.Good || c.CurrentState.State == CollectorState.None || c.CurrentState.State == CollectorState.Disabled || c.CurrentState.State == CollectorState.NotInServiceWindow)) { globalState = CollectorState.Good; } //Error state else if (CollectorHosts.Count == CollectorHosts.Count(c => c.CurrentState.State == CollectorState.Error || c.CurrentState.State == CollectorState.ConfigurationError || c.CurrentState.State == CollectorState.Disabled || c.CurrentState.State == CollectorState.NotInServiceWindow)) { globalState = CollectorState.Error; } else { globalState = CollectorState.Warning; } AlertLevel globalAlertLevel = AlertLevel.Info; if (globalState == CollectorState.Error) { globalAlertLevel = AlertLevel.Error; } else if (globalState == CollectorState.Warning) { globalAlertLevel = AlertLevel.Warning; } #endregion sw.Restart(); SendNotifierAlert(globalAlertLevel, DetailLevel.Summary, "GlobalState"); sw.Stop(); PCSetNotifiersSendTime(sw.ElapsedMilliseconds); IsBusyPolling = false; CurrentState = globalState; return(globalState); }
static void Main(string[] args) { int n = 10000; Stopwatch stopWatch = new Stopwatch(); CancellationTokenSource cancelTokenSource = new CancellationTokenSource(); CancellationToken token = cancelTokenSource.Token; Task task1 = new Task(() => { for (int i = 0; i < n; i++) { if (token.IsCancellationRequested) { Console.WriteLine("Операция прервана"); return; } if (isSimple(i)) { Console.WriteLine(i); } } }); stopWatch.Start(); task1.Start(); Thread.Sleep(1000); cancelTokenSource.Cancel(); //2 Console.WriteLine("Status: " + task1.Status); Console.WriteLine("isComplited: " + task1.IsCompleted); Console.WriteLine("Id: " + task1.Id); Task.WaitAll(task1); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); Task <string> tsk1 = new Task <string>(() => { return("London is the"); }); Task <string> tsk2 = new Task <string>(() => { return("the capital"); }); Task <string> tsk3 = new Task <string>(() => { return("of Great Britan"); }); tsk1.Start(); tsk2.Start(); tsk3.Start(); Task.WaitAll(tsk1, tsk2, tsk3); //Task tsk4 = Task.WhenAll(tsk1, tsk2, tsk3).ContinueWith(t => { Console.WriteLine(tsk1.Result + " " + tsk2.Result + " " + tsk3.Result); }); //4 Task tsk4 = Task.Run(() => { Console.WriteLine(tsk1.Result + " " + tsk2.Result + " " + tsk3.Result); }); //3 Task <int> what = Task.Run(() => { return(5); }); var awaiter = what.GetAwaiter(); awaiter.OnCompleted(() => { Console.WriteLine("Task complited with result: " + what.Result); }); //4 Parallel.For(1, 10, sum); //5 ParallelLoopResult result = Parallel.ForEach <int>(new List <int>() { 1, 3, 5, 8 }, sum); //5 Parallel.Invoke(() => sum(100), () => { int[] mass = new int[n]; for (int i = 0; i < n; i++) { mass[i] = i; } } ); BlockingCollection <int> blockcoll = new BlockingCollection <int>(); for (int producer = 0; producer < 5; producer++) { Thread.Sleep(producer * 100); Task.Factory.StartNew(() => { blockcoll.Add(producer); Console.WriteLine("Поставщик " + producer); }); } Task consumer = Task.Factory.StartNew( () => { foreach (var item in blockcoll.GetConsumingEnumerable()) { Console.WriteLine(" Покупатель взял " + item); } }); SumAsync(); Console.Read(); }
public void displayBigBooba() { ParallelLoopResult result = Parallel.ForEach <int>(mas, BigBoobaa); }
private void LetsDoItButtonClick(object sender, EventArgs e) { myParallObj = new List <ParallObj>(); bool processFulfilled = false; try { foreach (var imgPath in pathsList) { ParallObj temp = new ParallObj(); temp.ImgPathObj = imgPath; temp.ImgQualityObj = ImgQtyDecreasing; temp.pathDestinationObj = setFilesDirPath; myParallObj.Add(temp); } } catch (Exception ex) { MessageBox.Show("Please, put correct data!"); return; } //Black&White and Decreased quality, synch processing if (getFilesDirPath != "" && setFilesDirPath != "" && pathsList != null && ImgQtyDecreasing != 0 && CheckBoxBlackWhite.IsChecked == true && synch.IsChecked == true && asynch.IsChecked == false) { var stopWatch = Stopwatch.StartNew(); foreach (var imageData in myParallObj) { Operating.SaveImgAndGray(imageData); } MessageBox.Show("Processing time is " + stopWatch.Elapsed.TotalSeconds.ToString() + " sec."); processFulfilled = true; } //Decreased quality, synch processing if (getFilesDirPath != "" && setFilesDirPath != "" && pathsList != null && ImgQtyDecreasing != 0 && CheckBoxBlackWhite.IsChecked == false && synch.IsChecked == true && asynch.IsChecked == false) { var stopWatch = Stopwatch.StartNew(); foreach (var imageData in myParallObj) { Operating.SaveImg(imageData); } MessageBox.Show("Processing time is " + stopWatch.Elapsed.TotalSeconds.ToString() + " sec."); processFulfilled = true; } //Decreased quality, asynch processing, ThreadPool if (getFilesDirPath != "" && setFilesDirPath != "" && pathsList != null && ImgQtyDecreasing != 0 && CheckBoxBlackWhite.IsChecked == false && synch.IsChecked == false && asynch.IsChecked == true && ThreadPoolBtn.IsChecked == true) { var stopWatch = Stopwatch.StartNew(); foreach (var element in myParallObj) { ThreadPool.QueueUserWorkItem(state => Operating.SaveImg(element)); } MessageBox.Show("Processing time is " + stopWatch.Elapsed.TotalSeconds.ToString() + " sec."); processFulfilled = true; } //Decreased quality, asynch processing, Parallel if (getFilesDirPath != "" && setFilesDirPath != "" && pathsList != null && ImgQtyDecreasing != 0 && CheckBoxBlackWhite.IsChecked == false && synch.IsChecked == false && asynch.IsChecked == true && ParallelBtn.IsChecked == true) { var stopWatch = Stopwatch.StartNew(); ParallelLoopResult result = Parallel.ForEach <ParallObj>(myParallObj, Operating.SaveImg); MessageBox.Show("Processing time is " + stopWatch.Elapsed.TotalSeconds.ToString() + " sec."); processFulfilled = true; } //Black&White and Decreased quality, asynch processing, ThreadPool if (getFilesDirPath != "" && setFilesDirPath != "" && pathsList != null && ImgQtyDecreasing != 0 && CheckBoxBlackWhite.IsChecked == true && asynch.IsChecked == true && ThreadPoolBtn.IsChecked == true) { var stopWatch = Stopwatch.StartNew(); foreach (var element in myParallObj) { ThreadPool.QueueUserWorkItem(state => Operating.SaveImgAndGray(element)); } MessageBox.Show("Processing time is " + stopWatch.Elapsed.TotalSeconds.ToString() + " sec."); processFulfilled = true; } //Black&White and Decreased quality, asynch processing, Parallel if (getFilesDirPath != "" && setFilesDirPath != "" && pathsList != null && ImgQtyDecreasing != 0 && CheckBoxBlackWhite.IsChecked == true && asynch.IsChecked == true && ParallelBtn.IsChecked == true) { var stopWatch = Stopwatch.StartNew(); ParallelLoopResult resultBandW = Parallel.ForEach <ParallObj>(myParallObj, Operating.SaveImgAndGray); MessageBox.Show("Processing time is " + stopWatch.Elapsed.TotalSeconds.ToString() + " sec."); processFulfilled = true; } if (processFulfilled == false) { MessageBox.Show("Please, put correct data! \nChoose all options again!"); } setFilesDirPath = ""; getFilesDirPath = ""; pathsList = null; myParallObj = null; }
public void LoadGame(RootObject gameinfo, RiotSharp.RiotApi api, RiotSharp.Region reg, long sid) { try { BluePanel.BeginInvoke(new MethodInvoker(delegate { BluePanel.Controls.Clear(); RedPanel.Controls.Clear(); ginfo.Text = RiotTool.ToMapString(gameinfo.MapType) + ", " + RiotTool.ToQueueString(gameinfo.gameQueueConfigId) + " - " + RiotTool.PlatformToString(gameinfo.platformId); metroProgressSpinner1.Visible = true; B1.Visible = (gameinfo.bannedChampions.Count != 0); B2.Visible = (gameinfo.bannedChampions.Count != 0); B3.Visible = (gameinfo.bannedChampions.Count != 0); B4.Visible = (gameinfo.bannedChampions.Count != 0); B5.Visible = (gameinfo.bannedChampions.Count != 0); B6.Visible = (gameinfo.bannedChampions.Count != 0); foreach (BannedChampion b in gameinfo.bannedChampions) { Image champ = Image.FromFile(Application.StartupPath + @"\Champions\" + CurrentGame.GetChampion(b.ChampionId) + ".png"); switch (b.PickTurn) { case 1: B1.BackgroundImage = champ; break; case 3: B2.BackgroundImage = champ; break; case 5: B3.BackgroundImage = champ; break; case 2: B4.BackgroundImage = champ; break; case 4: B5.BackgroundImage = champ; break; case 6: B6.BackgroundImage = champ; break; } } })); int c = 0; frm.AnimationType = AnimatorNS.AnimationType.Mosaic; //foreach (Participant p in gameinfo.participants) //{ ParallelLoopResult pr = Parallel.ForEach(gameinfo.participants, p => { Player pl = new Player(p.TeamId == 200); pl.Dock = DockStyle.Top; LeaguePlayer pls = new LeaguePlayer(); pls.LoadPlayer(reg, api, p, CurrentGame.GetChampion(p.ChampionId)); if (pls.SummonerName == "Error occured") { pls.SummonerName = p.Name; } pl.LoadSummonerData(pls); MetroPanel panel = null; if (p.TeamId == 100) { panel = BluePanel; } else { panel = RedPanel; } pls.Dispose(); panel.Invoke(new MethodInvoker(delegate { //if (panel == BluePanel && p.TeamId != 100) // panel = RedPanel; c++; metroProgressSpinner1.Value = (int)((double)((double)c / (double)gameinfo.participants.Count) * 100); panel.Controls.Add(pl); //pl.Visible = false; //frm.Show(pl, Program.MainFormInstance.MainTabControl.SelectedTab == Program.MainFormInstance.GameInfoTab); })); // } }); while (!pr.IsCompleted) { Thread.Sleep(100); } RedPanel.Invoke(new MethodInvoker(delegate { this.RedPanel.Controls.Add(this.RedBan); this.BluePanel.Controls.Add(this.BlueBan); metroProgressSpinner1.Visible = false; metroProgressSpinner1.Value = 1; })); Program.MainFormInstance.ShowInfo("Game info", "The current game info are ready to be viewed"); if (SettingsManager.Settings.Speech) { SoundPlayer sp = new SoundPlayer(Application.StartupPath + @"\Data\done.wav"); sp.Load(); sp.PlaySync(); sp.Dispose(); } GC.Collect(); GC.WaitForPendingFinalizers(); } catch (Exception ex) { Logger.Instance.Log.Error("Failed to load game", ex); } }
static void Main(string[] args) { // Stopwatch object is defined here and used to measure the execution times of all loops. Stopwatch sw = new Stopwatch(); // Standard for loop iteration sw.Start(); for (int n = 0; n < 10000000; n++) { string converted = n.ToString(); if (n == 5) { break; } } Console.WriteLine($"Standard for loop takes {sw.ElapsedMilliseconds} milliseconds"); // Parallel For loop iteration sw.Restart(); ParallelLoopResult forResult = Parallel.For(0, 10000000, (n, state) => { string converted = n.ToString(); if (n == 5) { // Break method prevent any new iterations from being created. However, it allows any running iteration to complete. state.Break(); // Stop method immediately stops all running iteration and prevent any new iterations from being created. //state.Break(); } }); Console.WriteLine($"Parallel For loop takes {sw.ElapsedMilliseconds} milliseconds"); // Printing the properties of return value of For mehhod Console.WriteLine($"IsCompleted = {forResult.IsCompleted}"); Console.WriteLine($"LowestBreakIteration = {forResult.LowestBreakIteration}"); // Defining an array of URLs string[] urls = { "https://www.google.com", "https://www.microsoft.com", "https://www.visualstudio.com", "https://www.amazon.com", "https://www.youtube.com", "https://www.fb.com", "https://www.twitter.com" }; // Parallel Foreach loop sw.Restart(); Parallel.ForEach(urls, url => { HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "GET"; WebResponse myResponse = myRequest.GetResponse(); StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); string result = sr.ReadToEnd(); sr.Close(); myResponse.Close(); Console.WriteLine($" First 10 characters of '{url}' are \n {result.Substring(0, 10)}"); }); Console.WriteLine($"Parallel Foreach loop takes {sw.ElapsedMilliseconds} milliseconds"); // Standard foreach loop sw.Restart(); foreach (string url in urls) { HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "GET"; WebResponse myResponse = myRequest.GetResponse(); StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); string result = sr.ReadToEnd(); sr.Close(); myResponse.Close(); Console.WriteLine($" First 10 characters of '{url}' are \n {result.Substring(0, 10)}"); } Console.WriteLine($"Standard foreach loop takes {sw.ElapsedMilliseconds} milliseconds"); }
private static void PLRcheck(ParallelLoopResult plr, string ttype, bool shouldComplete, int?expectedLBI) { Assert.Equal(shouldComplete, plr.IsCompleted); Assert.Equal(expectedLBI, plr.LowestBreakIteration); }
unsafe void DeformationMeshParallel() { Vertex[] vertices = new Vertex[vertex_count]; ushort[] indices = new ushort[index_count]; mesh.SubMeshes[0].VertexData.GetSomeGeometry(ref changeable_vertex_pos, ref changeable_vertex_norm); if (first_init_mesh == false) { /*for (int i = 0; i < vertex_count; i++) * { * Vertex vertex = new Vertex(); * vertex.position = original_vertex_pos[i]; * vertex.normal = original_vertex_norm[i]; * vertex.texCoord = original_vertex_tc[i]; * vertices[i] = vertex; * } * for (int i = 0; i < index_count; i++) * { * indices[i] = (ushort)(original_vertex_ind[i]); * }*/ ParallelLoopResult parallel_result = Parallel.For(0, vertex_count, i => { Vertex vertex = new Vertex(); vertex.position = original_vertex_pos[i]; vertex.normal = original_vertex_norm[i]; vertex.texCoord = original_vertex_tc[i]; vertices[i] = vertex; }); if (parallel_result.IsCompleted) { for (int i = 0; i < index_count; i++) { indices[i] = (ushort)(original_vertex_ind[i]); } } SubMesh sub_mesh = mesh.SubMeshes[0]; { HardwareVertexBuffer vertex_buffer = sub_mesh.VertexData.VertexBufferBinding.GetBuffer(0); IntPtr buffer = vertex_buffer.Lock(HardwareBuffer.LockOptions.Discard); fixed(Vertex *pvertices = vertices) { NativeUtils.CopyMemory(buffer, (IntPtr)pvertices, vertices.Length * sizeof(Vertex)); } vertex_buffer.Unlock(); } { HardwareIndexBuffer index_buffer = sub_mesh.IndexData.IndexBuffer; IntPtr buffer = index_buffer.Lock(HardwareBuffer.LockOptions.Discard); fixed(ushort *pindices = indices) { NativeUtils.CopyMemory(buffer, (IntPtr)pindices, indices.Length * sizeof(ushort)); } index_buffer.Unlock(); } first_init_mesh = true; } else { if (hit_other != null) { ParallelLoopResult parallel_result = Parallel.For(0, vertex_count, i => { Vertex vertex = new Vertex(); Vec3 p = ((original_vertex_pos[i] * attached_mesh.ScaleOffset) * Rotation) + (Position + attached_mesh.PositionOffset); Vec3 pp = hit_pos; Sphere sp = new Sphere(pp, Type.CONF.DeformationRadius); Vec3 nvec = Vec3.Zero; Vec3 nnorm = Vec3.Zero; if (sp.IsContainsPoint(p)) { Ray ray = new Ray(p, changeable_vertex_norm[i] * .01f); if (!Single.IsNaN(ray.Direction.X) && !Single.IsNaN(ray.Origin.X)) { RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing( ray, (int)ContactGroup.CastOnlyDynamic);/*(int)ContactGroup.CastOnlyContact);*/ Vec3 collision_pos = Vec3.Zero; Vec3 collision_nor = Vec3.Zero; bool collision = false; foreach (RayCastResult result in piercingResult) { if (Array.IndexOf(PhysicsModel.Bodies, result.Shape.Body) != -1) { continue; } collision = true; collision_pos = result.Position; collision_nor = result.Normal; break; } if (collision) { float old_x = 0, old_y = 0, old_z = 0, new_x = 0, new_y = 0, new_z = 0; float deformation = 0, force = 0, max_deformation = Type.CONF.MaxStrengthDeformation, mass = 0, vel = 0; mass = hit_other.Body.Mass; vel = hit_other.Body.LastStepLinearVelocity.Length(); force = (((hit_this.Body.Mass * hit_this.Body.LastStepLinearVelocity.Length()) + (mass * vel)) / hit_this.Body.Mass) / 100.0f; if (force > max_deformation) { deformation = max_deformation; } else { deformation = force; } //Deform X if (changeable_vertex_pos[i].X > 0) { old_x = original_vertex_pos[i].X - deformation; if (old_x < changeable_vertex_pos[i].X) { new_x = old_x; } else { new_x = changeable_vertex_pos[i].X; } } else { old_x = original_vertex_pos[i].X + deformation; if (old_x > changeable_vertex_pos[i].X) { new_x = old_x; } else { new_x = changeable_vertex_pos[i].X; } } //Deform Y if (changeable_vertex_pos[i].Y > 0) { old_y = original_vertex_pos[i].Y - deformation; if (old_y < changeable_vertex_pos[i].Y) { new_y = old_y; } else { new_y = changeable_vertex_pos[i].Y; } } else { old_y = original_vertex_pos[i].Y + deformation; if (old_y > changeable_vertex_pos[i].Y) { new_y = old_y; } else { new_y = changeable_vertex_pos[i].Y; } } //Deform Z if (changeable_vertex_pos[i].Z > 0) { old_z = original_vertex_pos[i].Z - deformation; if (old_z < changeable_vertex_pos[i].Z) { new_z = old_z; } else { new_z = changeable_vertex_pos[i].Z; } } else { old_z = original_vertex_pos[i].Z + deformation; if (old_z > changeable_vertex_pos[i].Z) { new_z = old_z; } else { new_z = changeable_vertex_pos[i].Z; } } nvec = new Vec3(new_x, new_y, new_z); nnorm = -collision_nor; } else { nvec = changeable_vertex_pos[i]; nnorm = changeable_vertex_norm[i]; } } } else { nvec = changeable_vertex_pos[i]; nnorm = changeable_vertex_norm[i]; } vertex.position = nvec; vertex.normal = nnorm; vertex.texCoord = original_vertex_tc[i]; vertices[i] = vertex; }); if (parallel_result.IsCompleted) { for (int i = 0; i < index_count; i++) { indices[i] = (ushort)(original_vertex_ind[i]); } SubMesh sub_mesh = mesh.SubMeshes[0]; { HardwareVertexBuffer vertex_buffer = sub_mesh.VertexData.VertexBufferBinding.GetBuffer(0); IntPtr buffer = vertex_buffer.Lock(HardwareBuffer.LockOptions.Discard); fixed(Vertex *pvertices = vertices) { NativeUtils.CopyMemory(buffer, (IntPtr)pvertices, vertices.Length * sizeof(Vertex)); } vertex_buffer.Unlock(); } { HardwareIndexBuffer index_buffer = sub_mesh.IndexData.IndexBuffer; IntPtr buffer = index_buffer.Lock(HardwareBuffer.LockOptions.Discard); fixed(ushort *pindices = indices) { NativeUtils.CopyMemory(buffer, (IntPtr)pindices, indices.Length * sizeof(ushort)); } index_buffer.Unlock(); } } } hit_other = null; } }
/// <param name="type">0 is YAZ0, 1 is YAY0, anything else is CompressionHeader</param> public int Compress(VoidPtr srcAddr, int srcLen, Stream outStream, IProgressTracker progress, int type) { _pSrc = (byte *)srcAddr; _sourceLen = srcLen; int chunkCount = (int)Math.Ceiling((double)srcLen / _threadChunk); if (progress != null) { progress.Begin(0, srcLen, 0); } _contractions = new List <Contraction> [chunkCount]; bool YAY0Comp = type == 1; if (type == 0) { YAZ0 header = new YAZ0(); header._tag = YAZ0.Tag; header._unCompDataLen = (uint)_sourceLen; outStream.Write(&header, YAZ0.Size); } else if (type == 1) { //Don't write YAY0 header yet. //Collect all compression data first } else { CompressionHeader header = new CompressionHeader(); header.Algorithm = CompressionType.RunLength; header.ExpandedSize = (uint)_sourceLen; outStream.Write(&header, 4 + (header.LargeSize ? 4 : 0)); } ParallelLoopResult result = Parallel.For(0, chunkCount, FindContractions); while (!result.IsCompleted) { Thread.Sleep(100); } List <Contraction> fullContractions; int codeBits, current; byte codeByte; //byte[] temp; int lastUpdate = srcLen; fullContractions = new List <Contraction>(); for (int i = 0; i < _contractions.Length; i++) { fullContractions.AddRange(_contractions[i]); _contractions[i].Clear(); _contractions[i] = null; } _contractions = null; //temp = new byte[3 * 8]; codeBits = 0; codeByte = 0; current = 0; List <byte> tempCounts = new List <byte>(); List <byte> tempData = new List <byte>(); List <byte> codes = new List <byte>(); List <List <byte> > counts = new List <List <byte> >(); List <List <byte> > data = new List <List <byte> >(); for (int i = 0; i < srcLen;) { if (codeBits == 8) { codes.Add(codeByte); counts.Add(tempCounts); data.Add(tempData); tempCounts = new List <byte>(); tempData = new List <byte>(); codeBits = 0; codeByte = 0; } if (current < fullContractions.Count && fullContractions[current].Location == i) { if (fullContractions[current].Size >= 0x12) { byte b1 = (byte)(fullContractions[current].Offset >> 8), b2 = (byte)(fullContractions[current].Offset & 0xFF); if (YAY0Comp) { tempCounts.Add(b1); tempCounts.Add(b2); } else { tempData.Add(b1); tempData.Add(b2); } tempData.Add((byte)(fullContractions[current].Size - 0x12)); } else { byte b1 = (byte)((fullContractions[current].Offset >> 8) | ((fullContractions[current].Size - 2) << 4)), b2 = (byte)(fullContractions[current].Offset & 0xFF); if (YAY0Comp) { tempCounts.Add(b1); tempCounts.Add(b2); } else { tempData.Add(b1); tempData.Add(b2); } } i += fullContractions[current++].Size; while (current < fullContractions.Count && fullContractions[current].Location < i) { current++; } } else { codeByte |= (byte)(1 << (7 - codeBits)); tempData.Add(_pSrc[i++]); } codeBits++; if (progress != null) { if (i % 0x4000 == 0) { progress.Update(i); } } } codes.Add(codeByte); counts.Add(tempCounts); data.Add(tempData); if (YAY0Comp) { //Write header YAY0 header = new YAY0(); header._tag = YAY0.Tag; header._unCompDataLen = (uint)_sourceLen; uint offset = 0x10 + (uint)codes.Count; header._countOffset = offset; foreach (List <byte> list in counts) { offset += (uint)list.Count; } header._dataOffset = offset; outStream.Write(&header, YAY0.Size); //Write codes foreach (byte c in codes) { outStream.WriteByte(c); } //Write counts foreach (List <byte> list in counts) { outStream.Write(list.ToArray(), 0, list.Count); } //Write data foreach (List <byte> list in data) { outStream.Write(list.ToArray(), 0, list.Count); } } else { for (int i = 0; i < codes.Count; i++) { //Write code outStream.WriteByte(codes[i]); //Write data outStream.Write(data[i].ToArray(), 0, data[i].Count); } } outStream.Flush(); if (progress != null) { progress.Finish(); } return((int)outStream.Length); }
/// <summary> /// Used to verify the result of a loop that was 'Stopped' /// /// Expected: /// 1) A ParallelLoopResult with IsCompleted = false and LowestBreakIteration = null /// 2) For results that were processed, the body stored the correct value /// </summary> /// <param name="loopResult"></param> /// <returns></returns> private void StopVerification(ParallelLoopResult? loopResult) { Assert.False(loopResult == null, "No ParallelLoopResult returned"); Assert.False(loopResult.Value.IsCompleted == true || loopResult.Value.LowestBreakIteration != null, String.Format("ParallelLoopResult invalid, expecting Completed=false,LowestBreakIteration=null, actual: {0}, {1}", loopResult.Value.IsCompleted, loopResult.Value.LowestBreakIteration)); for (int i = 0; i < _parameters.Count; i++) Verify(i); }
public static async Task <IEnumerable <RepositoryContentWithCommitInfo> > TryLoadLinkedCommitDataAsync( [NotNull] Task <IReadOnlyList <RepositoryContent> > contentTask, [NotNull] String htmlUrl, [NotNull] GitHubClient client, long repoId, [NotNull] String branch, CancellationToken token) { // Try to download the file info IReadOnlyList <RepositoryContent> contents = null; try { // Load the full HTML body WebView view = new WebView(); TaskCompletionSource <String> tcs = new TaskCompletionSource <String>(); view.NavigationCompleted += (s, e) => { view.InvokeScriptAsync("eval", new[] { "document.documentElement.outerHTML;" }).AsTask().ContinueWith(t => { tcs.SetResult(t.Status == TaskStatus.RanToCompletion ? t.Result : null); }); }; // Manually set the user agent to get the full desktop site String userAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; ARM; Trident/7.0; Touch; rv:11.0; WPDesktop) like Gecko"; HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(htmlUrl)); httpRequestMessage.Headers.Append("User-Agent", userAgent); view.NavigateWithHttpRequestMessage(httpRequestMessage); // Run the web calls in parallel await Task.WhenAll(contentTask, tcs.Task); contents = contentTask.Result; String html = tcs.Task.Result; if (token.IsCancellationRequested) { return(contents?.OrderByDescending(entry => entry.Type.Value).Select(content => new RepositoryContentWithCommitInfo(content))); } // Load the HTML document HtmlDocument document = new HtmlDocument(); document.LoadHtml(html); /* ================= * HTML error tags * ================= * ... * <include-fragment class="commit-tease commit-loader"> * ... * <div class="loader-error"/> * </include-fragment> * ... */ // Check if the HTML loading was successful if (document.DocumentNode ?.Descendants("include-fragment") // Get the <include-fragment/> nodes ?.FirstOrDefault(node => node.Attributes?.AttributesWithName("class") // Get the nodes with a class attribute ?.FirstOrDefault(att => att.Value?.Equals("commit-tease commit-loader") == true) // That attribute must have this name != null) // There must be a node with these specs if the HTML loading failed ?.Descendants("div") // Get the inner <div/> nodes ?.FirstOrDefault(node => node.Attributes?.AttributesWithName("class")?.FirstOrDefault() // Check the class name ?.Value?.Equals("loader-error") == true) != null || // Make sure there was in fact a loading error html.Contains("class=\"warning include - fragment - error\"") || html.Contains("Failed to load latest commit information")) { System.Diagnostics.Debug.WriteLine("[DEBUG] Fallback"); // Use the Oktokit APIs to get the info IEnumerable <Task <IReadOnlyList <GitHubCommit> > > tasks = contents.Select(r => client.Repository.Commit.GetAll(repoId, new CommitRequest { Path = r.Path, Sha = branch }, // Only get the commits that edited the current file new ApiOptions { PageCount = 1, PageSize = 1 })); // Just get the latest commit for this file IReadOnlyList <GitHubCommit>[] commits = await Task.WhenAll(tasks); // Query the results return(contents.AsParallel().OrderByDescending(file => file.Type.Value).Select((file, i) => { GitHubCommit commit = commits[i].FirstOrDefault(); return commit != null ? new RepositoryContentWithCommitInfo(file, commit, null, commit.Commit.Committer.Date.DateTime) : new RepositoryContentWithCommitInfo(file); })); } System.Diagnostics.Debug.WriteLine("[DEBUG] HTML parsing"); /* ================ * HTML STRUCTURE * ================ * ... * <tr class="js-navigation-item"> * ... * <td class="content"> * <span ...> * <a href="CONTENT_URL">...</a> * </span> * </td> * <td class="message"> * <span ...> * [...]? * <a title="COMMIT_MESSAGE">...</a> * </span> * </td> * <td class="age"> * <span ...> * <time-ago datetime="EDIT_TIME">...</a> * </span> * </td> * ... */ // Try to extract the commit info, in parallel int cores = Environment.ProcessorCount; List <RepositoryContentWithCommitInfo>[] partials = (from i in Enumerable.Range(1, cores) let list = new List <RepositoryContentWithCommitInfo>() select list).ToArray(); ParallelLoopResult result = Parallel.For(0, cores, new ParallelOptions { MaxDegreeOfParallelism = cores }, workerId => { int max = contents.Count * (workerId + 1) / cores; for (int i = contents.Count * workerId / cores; i < max; i++) { // Find the right node RepositoryContent element = contents[i]; HtmlNode target = document.DocumentNode?.Descendants("a") ?.FirstOrDefault(child => child.Attributes?.AttributesWithName("id") ?.FirstOrDefault()?.Value?.EndsWith(element.Sha) == true); // Parse the node contents if (target != null) { // Get the commit and time nodes HtmlNode messageRoot = target.Ancestors("td")?.FirstOrDefault()?.Siblings()?.FirstOrDefault(node => node.Name.Equals("td")), timeRoot = messageRoot?.Siblings()?.FirstOrDefault(node => node.Name.Equals("td")); HtmlAttribute messageTitle = messageRoot?.Descendants("a") ?.Select(node => node.Attributes?.AttributesWithName("title")?.FirstOrDefault()) ?.FirstOrDefault(node => node != null), timestamp = timeRoot?.Descendants("time-ago")?.FirstOrDefault()?.Attributes?.AttributesWithName("datetime")?.FirstOrDefault(); // Fix the message, if present String message = messageTitle?.Value; if (message != null) { message = WebUtility.HtmlDecode(message); // Remove HTML-encoded characters message = Regex.Replace(message, @":[^:]+: ?| ?:[^:]+:", String.Empty); // Remove GitHub emojis } // Add the parsed contents if (timestamp?.Value != null) { DateTime time; if (DateTime.TryParse(timestamp.Value, out time)) { partials[workerId].Add(new RepositoryContentWithCommitInfo(element, null, message, time)); continue; } } partials[workerId].Add(new RepositoryContentWithCommitInfo(element, null, message)); continue; } partials[workerId].Add(new RepositoryContentWithCommitInfo(element)); } }); if (!result.IsCompleted) { throw new InvalidOperationException(); } return(partials.SelectMany(list => list).OrderByDescending(entry => entry.Content.Type.Value)); } catch { // Just return the original content without additional info return(contents?.OrderByDescending(entry => entry.Type.Value).Select(content => new RepositoryContentWithCommitInfo(content))); } }
/// <summary> /// This verification is called when we expect an exception from the test /// /// Expected: ParallelLoopResult is returned as null /// </summary> /// <param name="loopResult"></param> /// <returns></returns> private void ExceptionalVerification(ParallelLoopResult? loopResult) { Assert.Null(loopResult); }
/// <summary> /// 发送音视频数据 /// </summary> /// <param name="httpContexts"></param> /// <param name="data"></param> /// <param name="firstSend"></param> public void SendAVData(List <JT1078HttpContext> httpContexts, byte[] data, bool firstSend) { ParallelLoopResult parallelLoopResult = Parallel.ForEach(httpContexts, async(context) => { if (context.IsWebSocket) { if (firstSend) { context.FirstSend = firstSend; Sessions.TryUpdate(context.SessionId, context, context); } try { await context.WebSocketSendBinaryAsync(data); } catch (Exception ex) { if (Logger.IsEnabled(LogLevel.Information)) { Logger.LogInformation($"[ws close]:{context.SessionId}-{context.Sim}-{context.ChannelNo}-{context.StartTime:yyyyMMddhhmmss}"); } remove(context.SessionId); } } else { if (firstSend) { context.FirstSend = firstSend; Sessions.TryUpdate(context.SessionId, context, context); try { await context.HttpSendFirstChunked(data); } catch (Exception ex) { if (Logger.IsEnabled(LogLevel.Information)) { Logger.LogInformation($"[http close]:{context.SessionId}-{context.Sim}-{context.ChannelNo}-{context.StartTime:yyyyMMddhhmmss}"); } remove(context.SessionId); } } else { try { await context.HttpSendChunked(data); } catch (Exception ex) { if (Logger.IsEnabled(LogLevel.Information)) { Logger.LogInformation($"[http close]:{context.SessionId}-{context.Sim}-{context.ChannelNo}-{context.StartTime:yyyyMMddhhmmss}"); } remove(context.SessionId); } } } }); if (parallelLoopResult.IsCompleted) { } }
protected void MonitorCallbacks(string callee, Action <T> doCallback) { ModuleProc PROC = new ModuleProc(this.DYN_MODULE_NAME, (callee.IsEmpty() ? "" : "(" + callee + ") ") + "InvokeCallback"); try { int length = _callbacks.Count; IList <int> faultCallbacks = new List <int>(); if (_logClients && doCallback != null) { Log.Info(PROC, string.Format("Sending {0} callback data to {1:D} clients.", callee, length)); } ParallelLoopResult pResult = Parallel.For(0, length, (i) => { if (this.Executor.IsShutdown) { return; } if (!(i >= 0 && i < _callbacks.Count)) { return; } try { T callback = _callbacks[i]; ICommunicationObject coCallback = callback as ICommunicationObject; // callback inserted not from wcf channel if (coCallback == null) { if (doCallback != null) { doCallback(callback); } } else { if (coCallback.State == CommunicationState.Opened) { try { if (doCallback != null) { doCallback(callback); } } catch { faultCallbacks.Add(i); } } else { faultCallbacks.Add(i); } } } catch (Exception ex) { Log.Exception(PROC, ex); } }); // remove all the faulted callbacks if (faultCallbacks.Count > 0) { lock (_callbacksLock) { foreach (var index in faultCallbacks) { this.RemoveCallback(index, null, true); } } // notify the lock status this.NotifyCallbackLock(); } } catch (Exception ex) { Log.Exception(PROC, ex); } }
/// <summary> /// This verification is used we successfully called 'Break' on the loop /// /// Expected: /// 1) A valid ParallelLoopResult was returned with IsCompleted = false & LowestBreakIteration = lowest iteration on which /// the test called Break /// 2) For results that were processed, the body stored the correct value /// </summary> /// <param name="loopResult"></param> /// <returns></returns> private void BreakVerification(ParallelLoopResult? loopResult) { Assert.False(loopResult == null, "No ParallelLoopResult returned"); Assert.False(loopResult.Value.IsCompleted == true || loopResult.Value.LowestBreakIteration == null || loopResult.Value.LowestBreakIteration != _lowestBreakIter, String.Format("ParallelLoopResult invalid, expecting Completed=false,LowestBreakIteration={0}, actual: {1}, {2}", _lowestBreakIter, loopResult.Value.IsCompleted, loopResult.Value.LowestBreakIteration)); for (int i = 0; i < _lowestBreakIter.Value - _startIndex; i++) Verify(i); }
public override bool Execute() { if (SourceFiles.Length == 0) { Log.LogError($"No SourceFiles to compile"); return(false); } ITaskItem?badItem = SourceFiles.FirstOrDefault(sf => string.IsNullOrEmpty(sf.GetMetadata("ObjectFile"))); if (badItem != null) { Log.LogError($"Source file {badItem.ItemSpec} is missing ObjectFile metadata."); return(false); } if (!Enum.TryParse(OutputMessageImportance, ignoreCase: true, out MessageImportance messageImportance)) { Log.LogError($"Invalid value for OutputMessageImportance={OutputMessageImportance}. Valid values: {string.Join(", ", Enum.GetNames(typeof(MessageImportance)))}"); return(false); } IDictionary <string, string> envVarsDict = GetEnvironmentVariablesDict(); ConcurrentBag <ITaskItem> outputItems = new(); try { Log.LogMessage(MessageImportance.Low, "Using environment variables:"); foreach (var kvp in envVarsDict) { Log.LogMessage(MessageImportance.Low, $"\t{kvp.Key} = {kvp.Value}"); } string workingDir = Environment.CurrentDirectory; Log.LogMessage(MessageImportance.Low, $"Using working directory: {workingDir}"); _tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(_tempPath); int allowedParallelism = Math.Min(SourceFiles.Length, Environment.ProcessorCount); #if false // Enable this when we bump msbuild to 16.1.0 if (BuildEngine is IBuildEngine9 be9) { allowedParallelism = be9.RequestCores(allowedParallelism); } #endif if (DisableParallelCompile || allowedParallelism == 1) { foreach (ITaskItem srcItem in SourceFiles) { if (!ProcessSourceFile(srcItem)) { return(false); } } } else { ParallelLoopResult result = Parallel.ForEach(SourceFiles, new ParallelOptions { MaxDegreeOfParallelism = allowedParallelism }, (srcItem, state) => { if (!ProcessSourceFile(srcItem)) { state.Stop(); } }); if (!result.IsCompleted && !Log.HasLoggedErrors) { Log.LogError("Unknown failed occured while compiling"); } } } finally { if (!string.IsNullOrEmpty(_tempPath)) { Directory.Delete(_tempPath, true); } } OutputFiles = outputItems.ToArray(); return(!Log.HasLoggedErrors); bool ProcessSourceFile(ITaskItem srcItem) { string srcFile = srcItem.ItemSpec; string objFile = srcItem.GetMetadata("ObjectFile"); try { string command = $"emcc {Arguments} -c -o \"{objFile}\" \"{srcFile}\""; // Log the command in a compact format which can be copy pasted StringBuilder envStr = new StringBuilder(string.Empty); foreach (var key in envVarsDict.Keys) { envStr.Append($"{key}={envVarsDict[key]} "); } Log.LogMessage(MessageImportance.Low, $"Exec: {envStr}{command}"); (int exitCode, string output) = Utils.RunShellCommand( Log, command, envVarsDict, workingDir: Environment.CurrentDirectory, logStdErrAsMessage: true, debugMessageImportance: messageImportance, label: Path.GetFileName(srcFile)); if (exitCode != 0) { Log.LogError($"Failed to compile {srcFile} -> {objFile}"); return(false); } ITaskItem newItem = new TaskItem(objFile); newItem.SetMetadata("SourceFile", srcFile); outputItems.Add(newItem); return(!Log.HasLoggedErrors); } catch (Exception ex) { Log.LogError($"Failed to compile {srcFile} -> {objFile}{Environment.NewLine}{ex.Message}"); return(false); } } }
private async Task RunDownloadParallelAsync(IProgress <ProgressModel> progress, ParallelOptions parallelOption) { var websites = SiteData(); int count = 1; ProgressModel model = new ProgressModel(); await Task.Run(() => { try { ParallelLoopResult result = Parallel.ForEach <string>(websites, parallelOption, (site, loopState) => { if (parallelOption.CancellationToken.IsCancellationRequested) { this.Dispatcher.Invoke(() => { resultsWindow.Text += $"The download was cancelled : {Environment.NewLine}"; }); return; } if (loopStatus == "stop") { loopState.Stop(); return; } else if (loopStatus == "break") { loopState.Break(); return; } var results = DownloadWebsite(site); model.PercentageComplete = (count * 100) / websites.Count; progress.Report(model); count++; this.Dispatcher.Invoke(() => { resultsWindow.Text += $"{results.WebsiteUrl} downloaded: {results.WebsiteData.Length}" + $" characters long. {Environment.NewLine}"; }); }); if (result.IsCompleted) { this.Dispatcher.Invoke(() => { resultsWindow.Text += "Stack ran to completion" + $" {Environment.NewLine}"; }); } else if (result.IsCompleted == false && result.LowestBreakIteration == null) { //Loop exited prematurely by Stop() this.Dispatcher.Invoke(() => { resultsWindow.Text += "Stack exited by Stop()." + $" {Environment.NewLine}"; loopStatus = "non"; }); } else if (result.IsCompleted == false && result.LowestBreakIteration != null) { //Loop exited by Break() this.Dispatcher.Invoke(() => { resultsWindow.Text += "Stack exited by Break()." + $"{Environment.NewLine}"; loopStatus = "non"; }); } } catch (OperationCanceledException) { this.Dispatcher.Invoke(() => { resultsWindow.Text += $"The download was cancelled : {Environment.NewLine}"; }); } finally { cts.Dispose(); cts = new CancellationTokenSource(); } }); }
private static void worker_DoWork(object sender, DoWorkEventArgs e) { args = (TagWriterWorkerArgs)e.Argument; // Assign args to static variable. This is mainly used for cancellation/reverting, to understand what files were targeted in the operation lastArgs = args; int processed = 0; int count = args.rows.Count(); int rowIndex = 0; // Misc int success = 0; // <- File was successfully written to int unmapped = 0; // <- Entry didn't have a file mapped to it int unsupportedFormat = 0; // <- File was of an unsupported format (UnsupportedFormatException) int corruptFile = 0; // <- File was corrupted (CorruptFileException) int ioError = 0; // <- Error occured while loading the file (IOException) int otherError = 0; // <- Another exception occurred Debug.Log(string.Format("\nWriting tags for {0} files, using the following parameters:\nfilter:\t\t{1}\nskipDateAdded:\t{3}\nskipLastPlayed:\t{4}\nskipPlayCount:\t{5}\nskipRating:\t{6}\nremoveTags:\t{7}\n\nUse Ctrl-C to terminate", count, args.filter, args.filterNot, args.skipDateAdded, args.skipLastPlayed, args.skipPlayCount, args.skipRating, args.removeTags)); // Exit here if we have nothing to process if (count == 0) { Debug.LogError("No entries to process!", true); return; } // Wait 3 seconds before starting if (Settings.Current.workerDelayStart) { for (int i = 0; i < 3; i++) { Console.Write("."); System.Threading.Thread.Sleep(1000); } } // A dictionary of entries and errors that have occured entryErrors = new ConcurrentDictionary <Entry, string>(); // Progress report counter, gets set to the sw.ElapsedMilliseconds at every report progress interval long progressReportCounter = 0; // Start a new stopwatch var sw = System.Diagnostics.Stopwatch.StartNew(); // Set the TaskbarProgressState TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal); Console.Write("\n\n"); void ReportProgress() { // Report progress to main thread to update DataGridView selection worker.ReportProgress(0, new ProgressArgs() { processed = processed, count = count, timeMs = sw.ElapsedMilliseconds, currentRowIndex = rowIndex, }); } int lowestBreakIndex = 0; // Set up the ParallelOptions var parallelOptions = new ParallelOptions() { MaxDegreeOfParallelism = Settings.Current.maxParallelThreads }; Resume: // Split the work into multiple parallel threads ParallelLoopResult parallelLoopResult = Parallel.ForEach(args.rows.Skip(lowestBreakIndex), parallelOptions, (row, parallelLoopState) => { // Firstly, do some processing stuffs that can only be done in the worker // Check if we should pause if (isPaused) { // Breaking allows current parallel iterations to complete before stopping parallelLoopState.Break(); return; } // Check if we should cancel if (worker.CancellationPending) { e.Cancel = true; parallelLoopState.Stop(); return; } // Check if we should report progress // Only report progress every <progressReportInterval> milliseconds if (Settings.Current.workerReportsProgress && sw.ElapsedMilliseconds - progressReportCounter > Settings.Current.workerReportProgressInterval) { rowIndex = row.Index; progressReportCounter = sw.ElapsedMilliseconds; ReportProgress(); } // Fetch the DataBoundItem from the DataGridViewRow var entry = (Entry)row.DataBoundItem; // Check if the entry hasn't already been processed in this session // This may be the case if we paused previously if (!entry.processed) { // Create a new DebugStack // We're unable to log from a parallel for, as the logs would be out of sync - and wouldn't be clustered together // So instead of logging normally, logging within a Parallel is done to a DebugStack, which essentially just collects the logs so that we can fire them all at once // This is done here instead of in WriteTagsForRow, as that method has many exit points, therefore we would have to return a debugstack object from it anyway var ds = new DebugStack(); // Write the tags for this Entry var result = WriteTagsForRow(entry, ++processed, ds); // Increment the error/success counter switch (result) { case TagWriterReturnCode.Success: success++; break; case TagWriterReturnCode.Unmapped: unmapped++; break; case TagWriterReturnCode.UnsupportedFormat: unsupportedFormat++; break; case TagWriterReturnCode.CorruptFile: corruptFile++; break; case TagWriterReturnCode.IOEError: ioError++; break; case TagWriterReturnCode.OtherError: otherError++; break; } // Set the processed flag on this entry to true entry.processed = true; // Log the DebugStack ds.Push(); } }); // If the Parallel.ForEach was paused, isPaused will be set if (isPaused) { // Save the index of the lowest last item processed lowestBreakIndex = (int)parallelLoopResult.LowestBreakIteration; Debug.Log("--- PAUSED ---"); sw.Stop(); // Set the taskbar ProgressState to Paused TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Paused); // Halt execution until unpaused (Reset) manualResetEvent.WaitOne(); // If we're here, the operation was unpaused // Handle cancelling while paused if (worker.CancellationPending || e.Cancel) { e.Cancel = true; return; } // Otherwise resume else { Debug.Log("--- RESUMED ---"); sw.Start(); TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal); // Jump back up to Resume goto Resume; } } // Return if we cancelled if (e.Cancel) { return; } sw.Stop(); // Do final ReportProgress rowIndex = args.rows.Last().Index; ReportProgress(); string resultStr = string.Format("Took {0}ms ({1})\n {2} files were written to successfully\n {3} files could not be written to\n {4} unsupported format\n {5} corrupt\n {6} IO errors\n {7} unmapped\n {8} other errors occurred", sw.ElapsedMilliseconds, TimeSpan.FromMilliseconds(sw.ElapsedMilliseconds).ToString("m\\:ss"), success, unsupportedFormat + corruptFile + ioError + unmapped + otherError, unsupportedFormat, corruptFile, ioError, unmapped, otherError); Debug.Log("Done! " + resultStr); mainForm.Invoke(() => mainForm.Flash(false)); System.Media.SystemSounds.Exclamation.Play(); MessageBox.Show(resultStr, "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information); if (args.removeTags) { Debug.Log("\n" + Consts.REMOVE_TAGS_FINISHED); } else { Debug.Log("\n" + Consts.WRITE_TAGS_FINISHED); } }