public static void CancelBeforeWait()
        {
            CountdownEvent countdownEvent = new CountdownEvent(2);
            CancellationTokenSource cs = new CancellationTokenSource();
            cs.Cancel();
            CancellationToken ct = cs.Token;

            const int millisec = 100;
            TimeSpan timeSpan = new TimeSpan(100);
            string message = "CancelBeforeWait:  > Cancellation token does not match.";
            EnsureOperationCanceledExceptionThrown(() => countdownEvent.Wait(ct), ct, message);
            EnsureOperationCanceledExceptionThrown(() => countdownEvent.Wait(millisec, ct), ct, message);
            EnsureOperationCanceledExceptionThrown(() => countdownEvent.Wait(timeSpan, ct), ct, message);

            countdownEvent.Dispose();
        }
Example #2
0
 // Tries some simple timeout cases.
 private static void RunCountdownEventTest1_SimpleTimeout(int ms)
 {
     // Wait on the event.
     CountdownEvent ev = new CountdownEvent(999);
     Assert.False(ev.Wait(ms));
     Assert.False(ev.IsSet);
     Assert.False(ev.WaitHandle.WaitOne(ms));
 }
        public static void RunPartitionerStaticTest_SingleChunking()
        {
            CountdownEvent cde = new CountdownEvent(2);
            Action[] actions = new Action[256];

            // The thinking here is that we'll put enough "filler" into this array to
            // insure that "natural" chunk size is greater than 2.  Without the
            // NoBuffering option, the Parallel.ForEach below is certain to deadlock.
            // Somewhere a Signal() is going to be after a Wait() in the same chunk, and
            // the loop will deadlock.
            for (int i = 0; i < 252; i++) actions[i] = () => { };
            actions[252] = () => { cde.Wait(); };
            actions[253] = () => { cde.Signal(); };
            actions[254] = () => { cde.Wait(); };
            actions[255] = () => { cde.Signal(); };

            Debug.WriteLine("    * We'll hang here if EnumerablePartitionerOptions.NoBuffering is not working properly");
            Parallel.ForEach(Partitioner.Create(actions, EnumerablePartitionerOptions.NoBuffering), item =>
            {
                item();
            });
        }
Example #4
0
        public static void For(int minInclusive, int maxExlusive, Action<int> action)
        {
            var countdownEvent = new CountdownEvent(maxExlusive - minInclusive);

            WaitCallback worker = delegate(object o)
            {
                action((int)o);
                countdownEvent.ReleaseOne();
            };

            for (var i = minInclusive; i < maxExlusive; i++)
            {
                ThreadPool.QueueUserWorkItem(worker, i);
            }

            countdownEvent.Wait();
        }
        public static void CancelAfterWait()
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationToken cancellationToken = cancellationTokenSource.Token;

            CountdownEvent countdownEvent = new CountdownEvent(2); ;  // countdownEvent that will block all waiters

            Task.Run(() =>
            {
                cancellationTokenSource.Cancel();
            });

            //Now wait.. the wait should abort and an exception should be thrown
            EnsureOperationCanceledExceptionThrown(() => countdownEvent.Wait(cancellationToken), cancellationToken,
               "CancelAfterWait:  An OCE(null) should have been thrown that references the cancellationToken.");

            // the token should not have any listeners.
            // currently we don't expose this.. but it was verified manually
        }
Example #6
0
        /// <summary>
        /// Creates the reader and processor threads, waits for them to finish
        /// the work and then prints the sum of the results from the result queue.
        /// </summary>
        static void Main()
        {
            _allThreadsDone = new CountdownEvent(DegreeOfParallelism);

            new Thread(Reader).Start();
            for (int i = 0; i < DegreeOfParallelism; ++i)
            {
                new Thread(Processor).Start();
            }

            _allThreadsDone.Wait();

            int sum = 0;
            //Here we can use UnsafeItems to access the queue because we know
            //for sure that all concurrent work on it has finished.
            foreach (int wc in _wordCounts.UnsafeItems)
            {
                sum += wc;
            }
            Console.WriteLine("Total words: " + sum);
        }
Example #7
0
        public async Task DisposeWatch()
        {
            var  connectionClosed = new AsyncManualResetEvent();
            var  eventsReceived   = new CountdownEvent(1);
            bool serverRunning    = true;

            using (var server = new MockKubeApiServer(testOutput, async httpContext =>
            {
                await WriteStreamLine(httpContext, MockKubeApiServer.MockPodResponse);

                while (serverRunning)
                {
                    await WriteStreamLine(httpContext, MockAddedEventStreamLine);
                }

                return(true);
            }))
            {
                var client = new Kubernetes(new KubernetesClientConfiguration
                {
                    Host = server.Uri.ToString()
                });

                var listTask = await client.ListNamespacedPodWithHttpMessagesAsync("default", watch : true);

                var events = new HashSet <WatchEventType>();

                var watcher = listTask.Watch <V1Pod, V1PodList>(
                    (type, item) =>
                {
                    events.Add(type);
                    eventsReceived.Signal();
                },
                    onClosed: connectionClosed.Set
                    );

                // wait at least an event
                await Task.WhenAny(Task.Run(() => eventsReceived.Wait()), Task.Delay(TestTimeout));

                Assert.True(
                    eventsReceived.CurrentCount == 0,
                    "Timed out waiting for events."
                    );

                Assert.NotEmpty(events);
                Assert.True(watcher.Watching);

                watcher.Dispose();

                events.Clear();

                // Let the server disconnect
                serverRunning = false;

                var timeout = Task.Delay(TestTimeout);

                while (!timeout.IsCompleted && watcher.Watching)
                {
                    await Task.Yield();
                }

                Assert.Empty(events);
                Assert.False(watcher.Watching);
                Assert.True(connectionClosed.IsSet);
            }
        }
        public void SudokuTest()
        {
            try
            {
                Handler      handler      = new DesktopHandler(new ClingoDesktopService(GetPath()));
                InputProgram inputProgram = new ASPInputProgram();

                for (int i = 0; i < N; i++)
                {
                    for (int j = 0; j < N; j++)
                    {
                        if (sudokuMatrix[i, j] != 0)
                        {
                            inputProgram.AddObjectInput(new Cell(i, j, sudokuMatrix[i, j]));
                        }
                    }
                }

                inputProgram.AddFilesPath(".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "test-resources" + Path.DirectorySeparatorChar + "asp" + Path.DirectorySeparatorChar + "sudoku");
                handler.AddProgram(inputProgram);
                handler.StartAsync(new CallbackAction(o =>
                {
                    if (!(o is AnswerSets))
                    {
                        return;
                    }

                    answerSets = (AnswerSets)o;

                    @lock.Signal();
                }));
                @lock.Wait(new TimeSpan(0, 0, 0, 0, 5000));
                Assert.IsNotNull(answerSets);
                Assert.IsTrue(String.IsNullOrEmpty(answerSets.ErrorsString), "Found error in the Plan\n" + answerSets.ErrorsString);

                if (answerSets.Answersets.Count == 0)
                {
                    return;
                }

                AnswerSet @as = answerSets.Answersets[0];

                foreach (object obj in @as.Atoms)
                {
                    Cell cell = (Cell)obj;
                    sudokuMatrix[cell.getRow(), cell.getColumn()] = cell.getValue();
                }

                for (int i = 0; i < N; i++)
                {
                    for (int j = 0; j < N; j++)
                    {
                        Console.Write(sudokuMatrix[i, j] + " ");

                        if (sudokuMatrix[i, j] == 0)
                        {
                            Assert.Fail("NumberNotValid");
                        }
                    }

                    Console.WriteLine();
                }
            }
            catch (Exception e)
            {
                Assert.Fail("Exception " + e.Message);
            }
        }
        public void RazerChromaApiGetsCalledOnGameEvents()
        {
            const string statusFile  = "Status.json";
            const string journalFile = "Journal.190101020000.01.log";

            using var cpl = ColoreProviderLock.GetLock();

            var chromaApi = new Mock <IChromaApi> {
                DefaultValue = DefaultValue.Mock
            };
            var mockIA   = chromaApi.Setup(x => x.InitializeAsync(It.IsAny <AppInfo>()));
            var mockCKEA = chromaApi.Setup(x => x.CreateKeyboardEffectAsync(It.IsAny <KeyboardEffect>(), It.IsAny <It.IsValueType>()));
            var mockUA   = chromaApi.Setup(x => x.UninitializeAsync());

            using TestFolder
                  dirRoot    = new TestFolder(_gameRootFolder),
                  dirOpts    = new TestFolder(_gameOptionsFolder),
                  dirJournal = new TestFolder();

            dirJournal.WriteText(statusFile, EventSequence.BuildEvent("Status", new { Flags = 0 }));
            dirJournal.WriteText(journalFile, EventSequence.BuildEvent("Fileheader", new { part = 1, language = @"English\UK", gameversion = "3.5.0.200 EDH", build = "r210198/r0 " }));

            using var cc = new ChromaController(dirRoot.Name, dirOpts.Name, dirJournal.Name)
                  {
                      ChromaFactory = new ChromaFactory
                      {
                          ChromaApi     = chromaApi.Object,
                          ChromaAppInfo = null,
                      },
                      AnimationFrameRate     = 0,
                      DetectGameInForeground = false,
                  };

            Assert.False(cc.DetectGameInForeground);

            using var ceIA = new CountdownEvent(1);
            mockIA.Callback(() => ceIA.Signal());

            using var ceCKEA = new CountdownEvent(1);
            mockCKEA.Callback(() => ceCKEA.Signal());

            cc.Start();

            Assert.True(ceIA.Wait(1000));
            Assert.True(ceCKEA.Wait(1000));

            var seq = BuildEventSequence();

            ceCKEA.Reset(seq.Count(x => x.ChangesGameState));

            seq.Play(dirJournal, journalFile, statusFile);

            Assert.True(ceCKEA.Wait(200 * seq.Count));

            using var ceUA = new CountdownEvent(1);
            mockUA.Callback(() => ceUA.Signal());

            cc.Stop();

            Assert.True(ceUA.Wait(1000));
        }
Example #10
0
        private static double RunInParallel <TData, TResult>(
            ThreadLocal <BatchPredictionEngine <TData, TResult> > engine,
            List <TData[]> batches,
            IExecutor[] executors,
            int numThreadsPerExecutor,
            int numRequestors,
            int numRepeat)
            where TData : class, new()
            where TResult : class, new()
        {
            CountdownEvent warmupCde = new CountdownEvent(executors.Length * numThreadsPerExecutor);

            foreach (var executor in executors)
            {
                for (int i = 0; i < numThreadsPerExecutor; i++)
                {
                    // Does it make sure we cache?
                    executor.Submit(() =>
                    {
                        var result    = engine.Value.Predict(batches[0], false);
                        var resultStr = string.Join(',', result.Select(x =>
                        {
                            if (x is AttendeeResult)
                            {
                                return((x as AttendeeResult).Score);
                            }
                            else if (x is AmazonResult)
                            {
                                return((x as AmazonResult).Score);
                            }
                            else
                            {
                                throw new Exception("Unsupported result type");
                            }
                        }));
                        warmupCde.Signal();
                    });
                }
            }
            warmupCde.Wait();

            CountdownEvent outerCde = new CountdownEvent(numRepeat * batches.Count);
            CountdownEvent cde      = new CountdownEvent(1);

            Action compute = () =>
            {
                cde.Wait();

                for (int bIdx = 0; bIdx < numRepeat * batches.Count / numRequestors; bIdx++)
                {
                    // Internally an executor is chosen in round-robin.
                    executors[bIdx % executors.Length].Submit(() =>
                    {
                        //results.Enqueue(engine.Predict(batches[bIdx % batches.Count]));
                        var result    = engine.Value.Predict(batches[bIdx % batches.Count], true);
                        var resultStr = string.Join(',', result.Select(x =>
                        {
                            if (x is AttendeeResult)
                            {
                                return((x as AttendeeResult).Score);
                            }
                            else if (x is AmazonResult)
                            {
                                return((x as AmazonResult).Score);
                            }
                            else
                            {
                                throw new Exception("Unsupported result type");
                            }
                        }));
                        outerCde.Signal();
                    });
                }
            };

            int cores = RegisterRequestorThreads(compute, numRequestors);

            var startTime = Stopwatch.GetTimestamp();

            cde.Signal();

            outerCde.Wait();
            var elapsedTime = (Stopwatch.GetTimestamp() - startTime) * 1000.0 / Stopwatch.Frequency;

            System.Console.WriteLine("Prediction took {0} ms", elapsedTime);

            return(elapsedTime);
        }
        public async Task RunsTasksConcurrently(int concurrentTasks)
        {
            // Control + 3 experiments
            var totalTasks = 1 + 3;

            // Expected number of batches
            var expectedBatches = Math.Ceiling(1D * totalTasks / concurrentTasks);

            // Use CountdownEvents to ensure tasks don't finish before all tasks in that batch have started
            var startedSignal  = new CountdownEvent(concurrentTasks);
            var finishedSignal = new CountdownEvent(concurrentTasks);

            // Batch counter
            int batch = 1;

            // Our test task
            var task = new Func <Task <KeyValuePair <int, int> > >(() =>
            {
                return(Task.Run(() =>
                {
                    // Signal that we have started
                    var last = startedSignal.Signal();

                    var myBatch = batch;

                    // Wait till all tasks for this batch have started
                    startedSignal.Wait();

                    // Signal we have finished
                    finishedSignal.Signal();

                    // Last task to start needs to reset the events
                    if (last)
                    {
                        // Wait for all tasks in the batch to have finished
                        finishedSignal.Wait();

                        // Reset the countdown events
                        startedSignal.Reset();
                        finishedSignal.Reset();
                        batch++;
                    }

                    // Return threadId
                    return new KeyValuePair <int, int>(myBatch, Thread.CurrentThread.ManagedThreadId);
                }));
            });

            // Run the experiment
            string experimentName = nameof(RunsTasksConcurrently) + concurrentTasks;
            await Scientist.ScienceAsync <KeyValuePair <int, int> >(experimentName, concurrentTasks, experiment =>
            {
                // Add our control and experiments
                experiment.Use(task);
                for (int idx = 2; idx <= totalTasks; idx++)
                {
                    experiment.Try($"experiment{idx}", task);
                }
            });

            // Get the test result
            var result = TestHelper.Results <KeyValuePair <int, int> >(experimentName).First();

            // Consolidate the returned values from the tasks
            var results = result.Observations.Select(x => x.Value);

            // Assert correct number of batches
            Assert.Equal(expectedBatches, results.Select(x => x.Key).Distinct().Count());

            // Now check each batch
            for (int batchNo = 1; batchNo <= expectedBatches; batchNo++)
            {
                // Get the threadIds used by each task in the batch
                var batchThreadIds = results.Where(x => x.Key == batchNo).Select(x => x.Value);

                // Assert expected number of concurrent tasks in batch
                Assert.Equal(concurrentTasks, batchThreadIds.Count());

                // Assert unique threadIds in batch
                Assert.Equal(batchThreadIds.Count(), batchThreadIds.Distinct().Count());
            }
        }
 public bool CheckLatch(CountdownEvent latch)
 {
     return(latch.Wait(AwaitTimeout));
 }
        public object Clone()
        {
            var filePaths = new List<string>();
            var fileRelativePaths = new List<string>();

            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
            try
            {
                var cssSpriteOutput = string.Empty;
                var jsOutput = string.Empty;
                var cssOutput = string.Empty;
                
                var countdownEvents = new CountdownEvent(3);

                ThreadPool.QueueUserWorkItem(data =>
                    {
                        var countdownEvent = (CountdownEvent) data;
                        try
                        {
                            if (_crusherConfiguration != null)
                            {
                                var cssSpriteGroups = _crusherConfiguration.CssSpriteGroups;
                                var cssSpriteCreator = new CssSpriteCreator(_cacheManager, _retryableFileOpener, _pathProvider, _retryableFileWriter, _fileMetaData);
                                var cssSpriteGroupsProcessor = new CssSpriteGroupsProcessor();

                                cssSpriteOutput = cssSpriteGroupsProcessor.ProcessGroups(_pathProvider, cssSpriteCreator, cssSpriteGroups).ToString();

                                var cssFilePaths = new List<string>();
                                var cssFileRelativePaths = new List<string>();
                                foreach (CssSpriteGroupElement cssSpriteGroup in cssSpriteGroups)
                                {
                                    cssFilePaths.Add(new Uri(_pathProvider.MapPath(cssSpriteGroup.CssOutputFilePath)).LocalPath);
                                    cssFilePaths.Add(new Uri(_pathProvider.MapPath(cssSpriteGroup.ImageOutputFilePath)).LocalPath);

                                    cssFileRelativePaths.Add(MakeRelative(cssSpriteGroup.CssOutputFilePath));
                                    cssFileRelativePaths.Add(MakeRelative(cssSpriteGroup.ImageOutputFilePath));
                                }

                                filePaths.AddRange(cssFilePaths);
                                fileRelativePaths.AddRange(cssFileRelativePaths);

                                _logMessage(cssSpriteOutput);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logError(exception.ToString());
                        }
                        countdownEvent.Signal();
                    }, countdownEvents);

                ThreadPool.QueueUserWorkItem(data =>
                    {
                        var countdownEvent = (CountdownEvent) data;

                        try
                        {
                            if (_crusherConfiguration != null)
                            {
                                var jsCrusher = new JsCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, _fileMetaData);
                                var jsGroups = _crusherConfiguration.JsGroups;
                                var jsGroupsProcessor = new JsGroupsProcessor();
                                jsOutput = jsGroupsProcessor.ProcessGroups(_pathProvider, jsCrusher, jsGroups).ToString();

                                var jsFilePaths = new List<string>();
                                var jsFileRelativePaths = new List<string>();
                                foreach (JsGroupElement jsGroup in jsGroups)
                                {
                                    jsFilePaths.Add(new Uri(_pathProvider.MapPath(jsGroup.OutputFilePath)).LocalPath);
                                    jsFileRelativePaths.Add(MakeRelative(jsGroup.OutputFilePath));
                                }

                                filePaths.AddRange(jsFilePaths);
                                fileRelativePaths.AddRange(jsFileRelativePaths);

                                _logMessage(jsOutput);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logError(exception.ToString());
                        }
                        countdownEvent.Signal();
                    }, countdownEvents);

                ThreadPool.QueueUserWorkItem(data =>
                    {
                        var countdownEvent = (CountdownEvent) data;

                        try
                        {
                            if (_crusherConfiguration != null)
                            {
                                var hashQueryStringKeyName = _crusherConfiguration.QuerystringKeyName;
                                var cssAssetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, _hasher, _pathProvider);
                                var cssPathRewriter = new CssPathRewriter(cssAssetsFileHasher, _pathProvider);
                                var cssCrusher = new CssCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, cssPathRewriter, _fileMetaData, _crusherConfiguration.WatchAssets);
                                var cssGroups = _crusherConfiguration.CssGroups;
                                var cssGroupsCrusher = new CssGroupsProcessor();
                                cssOutput = cssGroupsCrusher.ProcessGroups(_pathProvider, cssCrusher, cssGroups).ToString();

                                var cssFilePaths = new List<string>();
                                var cssFileRelativePaths = new List<string>();

                                foreach (CssGroupElement cssGroup in cssGroups)
                                {
                                    cssFilePaths.Add(new Uri(_pathProvider.MapPath(cssGroup.OutputFilePath)).LocalPath);
                                    cssFileRelativePaths.Add(MakeRelative(cssGroup.OutputFilePath));
                                }

                                filePaths.AddRange(cssFilePaths);
                                fileRelativePaths.AddRange(cssFileRelativePaths);

                                _logMessage(cssOutput);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logError(exception.ToString());
                        }
                        countdownEvent.Signal();
                    }, countdownEvents);

                countdownEvents.Wait();
            }
            catch (Exception exception)
            {
                _logError(exception.ToString());
            }
            finally
            {
                AppDomain.CurrentDomain.UnhandledException -= OnUnhandledException;
            }

            _setOutputFilePaths(filePaths.ToArray());
            _setOutputFileRelativePaths(fileRelativePaths.ToArray());

            return null;
        }
Example #14
0
        public async Task Concurrent_callers_to_Next_only_result_in_one_non_cancelled_token_being_issued()
        {
            const int loopCount = 10000;

            var logicalProcessorCount = Environment.ProcessorCount;
            var threadCount           = Math.Max(2, logicalProcessorCount);

            using (var sequence = new CancellationTokenSequence())
                using (var barrier = new Barrier(threadCount))
                    using (var countdown = new CountdownEvent(loopCount * threadCount))
                    {
                        var completedCount = 0;

                        var completionTokenSource = new CancellationTokenSource();
                        var completionToken       = completionTokenSource.Token;
                        var winnerByIndex         = new int[threadCount];

                        var tasks = Enumerable
                                    .Range(0, threadCount)
                                    .Select(i => Task.Run(() => ThreadMethod(i)))
                                    .ToList();

                        Assert.True(
                            countdown.Wait(TimeSpan.FromSeconds(10)),
                            "Test should have completed within a reasonable amount of time");

                        await Task.WhenAll(tasks);

                        Assert.AreEqual(loopCount, completedCount);

                        await Console.Out.WriteLineAsync("Winner by index: " + string.Join(",", winnerByIndex));

                        // Assume hyper threading, so halve logical processors (could use WMI or P/Invoke for more robust answer)
                        if (logicalProcessorCount <= 2)
                        {
                            Assert.Inconclusive("This test requires more than one physical processor to run.");
                        }

                        return;

                        void ThreadMethod(int i)
                        {
                            while (true)
                            {
                                barrier.SignalAndWait();

                                if (completionToken.IsCancellationRequested)
                                {
                                    return;
                                }

                                var token = sequence.Next();

                                barrier.SignalAndWait();

                                if (!token.IsCancellationRequested)
                                {
                                    Interlocked.Increment(ref completedCount);
                                    Interlocked.Increment(ref winnerByIndex[i]);
                                }

                                if (countdown.Signal())
                                {
                                    completionTokenSource.Cancel();
                                }
                            }
                        }
                    }
        }
        public static ParallelTaskResult StaticFor(int low, int high,
            Action<ParallelTaskOptions, int, int> work, int workerCount, int chunk)
        {
            if (low < 0 || high < low) return new ParallelTaskResult();

            ModuleProc PROC = new ModuleProc("ParallelTasks", "StaticFor");
            ParallelTaskResult result = new ParallelTaskResult(ParallelTaskResultStatus.Created);
            if (workerCount <= 0) workerCount = 1;
            if (chunk <= 0) chunk = 1;
            if (high < chunk) chunk = ((high - low) / workerCount);

            CountdownEvent cde = new CountdownEvent(workerCount);
            Thread[] threads = new Thread[workerCount];
            int currentCount = 0;
            ParallelTaskOptions options = new ParallelTaskOptions();

            try
            {
                for (int i = 0; i < workerCount; i++)
                {
                    threads[i] = Extensions.CreateThreadAndStart((o) =>
                    {
                        int k = (int)o;
                        int start = low + (k * chunk);
                        int end = ((k == (workerCount - 1)) ? high : (start + chunk));

                        for (int j = start; j < end; j++)
                        {
                            if (options.IsCancelled) break;

                            try
                            {
                                work(options, j, currentCount);
                            }
                            catch (Exception ex)
                            {
                                Log.Exception(PROC, ex);
                                result.Exceptions.Add(ex);
                            }
                            finally
                            {
                                Interlocked.Increment(ref currentCount);
                            }
                        }

                        cde.Signal();
                    }, i, "StaticFor_" + i.ToString());
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
            finally
            {
                cde.Wait();
                result.Status = (options.IsCancelled ? ParallelTaskResultStatus.Canceled : ParallelTaskResultStatus.Completed);
            }

            return result;
        }
        public static ParallelTaskResult DynamicFor(int low, int high,
            Action<ParallelTaskOptions, int, int> work, int workerCount)
        {
            if (low < 0 || high < low) return new ParallelTaskResult();

            ModuleProc PROC = new ModuleProc("ParallelTasks", "For");
            ParallelTaskResult result = new ParallelTaskResult(ParallelTaskResultStatus.Created);
            if (workerCount <= 0) workerCount = 1;
            const int chunk = 16;

            CountdownEvent cde = new CountdownEvent(workerCount);
            Thread[] threads = new Thread[workerCount];
            int currentCount = 0;
            int currentValue = low;
            ParallelTaskOptions options = new ParallelTaskOptions();

            try
            {
                for (int i = 0; i < workerCount; i++)
                {
                    threads[i] = Extensions.CreateThreadAndStart((o) =>
                    {
                        int j = 0;
                        int currentChunk = 1;

                        while (true)
                        {
                            if (options.IsCancelled) break;
                            if ((currentValue + currentChunk) > Int32.MaxValue) break;
                            j = Interlocked.Add(ref currentValue, currentChunk) - currentChunk;
                            if (j >= high) break;

                            for (int k = 0; (k < currentChunk) && ((j + k) < high); k++)
                            {
                                if (options.IsCancelled) break;

                                try
                                {
                                    work(options, j, currentCount);
                                }
                                catch (Exception ex)
                                {
                                    Log.Exception(PROC, ex);
                                }
                                finally
                                {
                                    Interlocked.Increment(ref currentCount);
                                }
                            }
                            if (currentChunk < chunk) currentChunk *= 2;
                        }

                        cde.Signal();
                    }, i);
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
            finally
            {
                cde.Wait();
                result.Status = (options.IsCancelled ? ParallelTaskResultStatus.Canceled : ParallelTaskResultStatus.Completed);
            }

            return result;
        }
Example #17
0
        public static void RunCountdownEventTest2_Exceptions()
        {
            CountdownEvent cde = null;
            Assert.Throws<ArgumentOutOfRangeException>(() => cde = new CountdownEvent(-1));
            // Failure Case: Constructor didn't throw AORE when -1 passed

            cde = new CountdownEvent(1);
            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Signal(0));
            // Failure Case: Signal didn't throw AORE when 0 passed

            cde = new CountdownEvent(0);
            Assert.Throws<InvalidOperationException>(() => cde.Signal());
            // Failure Case: Signal didn't throw IOE when the count is zero

            cde = new CountdownEvent(1);
            Assert.Throws<InvalidOperationException>(() => cde.Signal(2));
            // Failure Case: Signal didn't throw IOE when the signal count > current count

            Assert.Throws<ArgumentOutOfRangeException>(() => cde.AddCount(0));
            // Failure Case: AddCount didn't throw AORE when 0 passed

            cde = new CountdownEvent(0);
            Assert.Throws<InvalidOperationException>(() => cde.AddCount(1));
            // Failure Case: AddCount didn't throw IOE when the count is zero

            cde = new CountdownEvent(int.MaxValue - 10);
            Assert.Throws<InvalidOperationException>(() => cde.AddCount(20));
            // Failure Case: AddCount didn't throw IOE when the count > int.Max

            cde = new CountdownEvent(2);
            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Reset(-1));
            // Failure Case: Reset didn't throw AORE when the count is zero

            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(-2));
            // Failure Case: Wait(int) didn't throw AORE when the totalmilliseconds < -1

            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(TimeSpan.FromDays(-1)));
            // Failure Case:  FAILED.  Wait(TimeSpan) didn't throw AORE when the totalmilliseconds < -1

            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(TimeSpan.MaxValue));
            // Failure Case: Wait(TimeSpan, CancellationToken) didn't throw AORE when the totalmilliseconds > int.max

            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(TimeSpan.FromDays(-1), new CancellationToken()));
            // Failure Case: Wait(TimeSpan) didn't throw AORE when the totalmilliseconds < -1

            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(TimeSpan.MaxValue, new CancellationToken()));
            // Failure Case: Wait(TimeSpan, CancellationToken) didn't throw AORE when the totalmilliseconds > int.max

            cde.Dispose();

            Assert.Throws<ObjectDisposedException>(() => cde.Wait());
            // Failure Case: Wait() didn't throw ODE after Dispose
        }
Example #18
0
        private void InitManager()
        {
            AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;

            var jsExceptions = new List<JsException>();
            var cssExceptions = new List<CssException>();
            var countdownEvents = new CountdownEvent(2);

            ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        var groupsProcessor = new JsGroupsProcessor();
                        groupsProcessor.ProcessGroups(_pathProvider, _jsCrusher, _jsGroups);
                    }
                    catch (Exception exception)
                    {
                        jsExceptions.Add(new JsException(exception));
                    }
                    manualResetEvent.Signal();

                }, countdownEvents);

            ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        var groupsProcessor = new CssGroupsProcessor();
                        groupsProcessor.ProcessGroups(_pathProvider, _cssCrusher, _cssGroups);
                    }
                    catch (Exception exception)
                    {
                        cssExceptions.Add(new CssException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

            countdownEvents.Wait();

            var exceptions = cssExceptions.Cast<Exception>().Concat(jsExceptions.Cast<Exception>()).ToList();

            if (exceptions.Any())
            {
                throw new AggregateException(exceptions);
            }
        }
Example #19
0
            //---------------------------------------------------------------------------------------
            // Straightforward IEnumerator<T> methods.
            //

            internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TSource currentElement, ref int currentKey)
            {
                Debug.Assert(_source != null);

                if (_alreadySearched)
                {
                    return(false);
                }

                // Look for the greatest element.
                TSource candidate      = default(TSource) !;
                TKey    candidateKey   = default(TKey) !;
                bool    candidateFound = false;

                try
                {
                    int     loopCount = 0; //counter to help with cancellation
                    TSource value     = default(TSource) !;
                    TKey    key       = default(TKey) !;
                    while (_source.MoveNext(ref value !, ref key))
                    {
                        if ((loopCount & CancellationState.POLL_INTERVAL) == 0)
                        {
                            _cancellationToken.ThrowIfCancellationRequested();
                        }
                        ;

                        // If the predicate is null or the current element satisfies it, we will remember
                        // it as the current partition's candidate for the last element, and move on.
                        if (_predicate == null || _predicate(value))
                        {
                            candidate      = value;
                            candidateKey   = key;
                            candidateFound = true;
                        }

                        loopCount++;
                    }

                    // If we found a candidate element, try to publish it, so long as it's greater.
                    if (candidateFound)
                    {
                        lock (_operatorState)
                        {
                            if (_operatorState._partitionId == -1 || _keyComparer.Compare(candidateKey, _operatorState._key) > 0)
                            {
                                _operatorState._partitionId = _partitionId;
                                _operatorState._key         = candidateKey;
                            }
                        }
                    }
                }
                finally
                {
                    // No matter whether we exit due to an exception or normal completion, we must ensure
                    // that we signal other partitions that we have completed.  Otherwise, we can cause deadlocks.
                    _sharedBarrier.Signal();
                }

                _alreadySearched = true;

                // Only if we have a candidate do we wait.
                if (_partitionId == _operatorState._partitionId)
                {
                    _sharedBarrier.Wait(_cancellationToken);

                    // Now re-read the shared index. If it's the same as ours, we won and return true.
                    if (_operatorState._partitionId == _partitionId)
                    {
                        currentElement = candidate;
                        currentKey     = 0; // 1st (and only) element, so we hardcode the output index to 0.
                        return(true);
                    }
                }

                // If we got here, we didn't win. Return false.
                return(false);
            }
        private void Test(int[] results_sizes, string instance_name)
        {
            string instance_path = base_path + instance_name + Path.DirectorySeparatorChar;

            Console.WriteLine("Testing " + results_sizes.Length + " files for " + instance_path);

            for (int i = 1; i <= results_sizes.Length; i++)
            {
                try
                {
                    plan  = null;
                    @lock = new CountdownEvent(1);
                    Handler handler = new DesktopHandler(new SPDDesktopService());

                    Console.WriteLine();

                    InputProgram inputProgramDomain = new PDDLInputProgram(PDDLProgramType.DOMAIN);

                    inputProgramDomain.AddFilesPath(instance_path + "domain.pddl");

                    InputProgram inputProgramProblem = new PDDLInputProgram(PDDLProgramType.PROBLEM);
                    string       problem             = instance_path + "p" + (i < 10 ? "0" : "") + i + ".pddl";

                    Console.WriteLine(problem);
                    Assert.IsTrue(File.Exists(problem), "File not found: " + problem);
                    inputProgramProblem.AddFilesPath(problem);
                    handler.AddProgram(inputProgramDomain);
                    handler.AddProgram(inputProgramProblem);
                    PDDLMapper.Instance.RegisterClass(typeof(PickUp));
                    Assert.IsNull(plan);
                    handler.StartAsync(new CallbackAction(o =>
                    {
                        if (!(o is Plan))
                        {
                            return;
                        }

                        plan = (Plan)o;

                        foreach (embasp.languages.pddl.Action action in plan.Actions)
                        {
                            Console.Write(action.Name + ",");
                        }

                        @lock.Signal();
                    }));
                    @lock.Wait(new TimeSpan(0, 0, 0, 0, 15000));
                    Assert.IsNotNull(plan);

                    if (results_sizes[i - 1] != 0)
                    {
                        Assert.IsTrue(String.IsNullOrEmpty(plan.ErrorsString) || plan.ErrorsString.ToLower().Contains("server busy"), "Found error in the Plan " + problem + "\n" + plan.ErrorsString);
                    }

                    if (plan.ErrorsString.ToLower().Contains("server busy."))
                    {
                        Console.WriteLine("[WARNING] Server busy occurred!");
                    }

                    Assert.AreEqual(results_sizes[i - 1], plan.Actions.Count);

                    foreach (object obj in plan.ActionsObjects)
                    {
                        if (obj is PickUp)
                        {
                            PickUp pickUp = (PickUp)obj;
                            Console.WriteLine(pickUp.getBlock());
                        }
                    }

                    Thread.Sleep(500);
                }
                catch (Exception e)
                {
                    Assert.Fail(e.Message);
                }
            }

            Console.WriteLine();
        }
        /// <summary>
        /// The method that performs the tests
        /// Depending on the inputs different test code paths will be exercised
        /// </summary>
        internal void RealRun()
        {
            TaskScheduler tm = TaskScheduler.Default;

            CreateTask(tm, _taskTree);
            // wait the whole task tree to be created
            _countdownEvent.Wait();

            Stopwatch sw = Stopwatch.StartNew();

            try
            {
                switch (_api)
                {
                case API.Cancel:
                    _taskTree.CancellationTokenSource.Cancel();
                    break;

                case API.Wait:
                    switch (_waitBy)
                    {
                    case WaitBy.None:
                        _taskTree.Task.Wait();
                        _taskCompleted = true;
                        break;

                    case WaitBy.Millisecond:
                        _taskCompleted = _taskTree.Task.Wait(_waitTimeout);
                        break;

                    case WaitBy.TimeSpan:
                        _taskCompleted = _taskTree.Task.Wait(new TimeSpan(0, 0, 0, 0, _waitTimeout));
                        break;
                    }
                    break;
                }
            }
            catch (AggregateException exp)
            {
                _caughtException = exp.Flatten();
            }
            finally
            {
                sw.Stop();
            }

            if (_waitTimeout != -1)
            {
                long delta = sw.ElapsedMilliseconds - ((long)_waitTimeout + s_deltaTimeOut);

                if (delta > 0)
                {
                    Debug.WriteLine("ElapsedMilliseconds way more than requested Timeout.");
                    Debug.WriteLine("WaitTime= {0} ms, ElapsedTime= {1} ms, Allowed Descrepancy = {2} ms", _waitTimeout, sw.ElapsedMilliseconds, s_deltaTimeOut);
                    Debug.WriteLine("Delta= {0} ms", delta);
                }
                else
                {
                    var delaytask = Task.Delay((int)Math.Abs(delta));  // give delay to allow Context being collected before verification
                    delaytask.Wait();
                }
            }

            Verify();
            _countdownEvent.Dispose();
        }
Example #22
0
        private void InitManager()
        {
            AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;

            var jsExceptions = new List<JsException>(); 
            var cssExceptions = new List<CssException>();
            var countdownEvents = new CountdownEvent(2);

            ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        var jsGroups = _crusherConfiguration.JsGroups;
                        var jsCrusher = new JsCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, _fileMetaData);
                        var groupsProcessor = new JsGroupsProcessor();
                        groupsProcessor.ProcessGroups(_pathProvider, jsCrusher, jsGroups);
                    }
                    catch (Exception exception)
                    {
                        jsExceptions.Add(new JsException(exception));
                    }
                    manualResetEvent.Signal();

                }, countdownEvents);

            ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        var hashQueryStringKeyName = _crusherConfiguration.QuerystringKeyName;
                        var cssGroups = _crusherConfiguration.CssGroups;
                        var cssAssetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, _hasher, _pathProvider);
                        var cssPathRewriter = new CssPathRewriter(cssAssetsFileHasher, _pathProvider);
                        var cssCrusher = new CssCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, cssPathRewriter, _fileMetaData, _crusherConfiguration.WatchAssets);
                        var groupsProcessor = new CssGroupsProcessor();
                        groupsProcessor.ProcessGroups(_pathProvider, cssCrusher, cssGroups);
                    }
                    catch (Exception exception)
                    {
                        cssExceptions.Add(new CssException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

            countdownEvents.Wait();

            var exceptions = cssExceptions.Cast<Exception>().Concat(jsExceptions.Cast<Exception>()).ToList();

            if (exceptions.Any())
            {
                throw new AggregateException(exceptions);
            }
        }
        public void StopTest_MultiplyWaiters()
        {
            var target = CreateFixedThreadPool(2, false);
            var heavyTask = new TaskMock(Wait);
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.Medium));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.Low));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.Low));

            Exception deferedException = null;
            var waitersCounter = new CountdownEvent(4);

            for (var i = 0; i < 4; i++)
            {
                new Thread(() =>
                {
                    target.Stop();
                    try
                    { // each thread should see properly stopped pool.
                        AssertIsProperlyStopped(target);
                    }
                    catch (Exception e)
                    {
                        deferedException = e;
                    }
                    waitersCounter.Signal();
                }).Start();
            }

            target.Stop();
            AssertIsProperlyStopped(target);

            waitersCounter.Wait(); // if thread pool works properly, will never blocked here

            if (deferedException != null)
            {
                throw deferedException;
            }
        }
        public virtual void TestStressMultiThreading()
        {
            Directory         dir    = NewDirectory();
            IndexWriterConfig conf   = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()));
            IndexWriter       writer = new IndexWriter(dir, conf);

            // create index
            int numThreads = TestUtil.NextInt(Random(), 3, 6);
            int numDocs    = AtLeast(2000);

            for (int i = 0; i < numDocs; i++)
            {
                Document doc = new Document();
                doc.Add(new StringField("id", "doc" + i, Store.NO));
                double group = Random().NextDouble();
                string g;
                if (group < 0.1)
                {
                    g = "g0";
                }
                else if (group < 0.5)
                {
                    g = "g1";
                }
                else if (group < 0.8)
                {
                    g = "g2";
                }
                else
                {
                    g = "g3";
                }
                doc.Add(new StringField("updKey", g, Store.NO));
                for (int j = 0; j < numThreads; j++)
                {
                    long value = Random().Next();
                    doc.Add(new BinaryDocValuesField("f" + j, TestBinaryDocValuesUpdates.ToBytes(value)));
                    doc.Add(new NumericDocValuesField("cf" + j, value * 2)); // control, always updated to f * 2
                }
                writer.AddDocument(doc);
            }

            CountdownEvent done       = new CountdownEvent(numThreads);
            AtomicInt32    numUpdates = new AtomicInt32(AtLeast(100));

            // same thread updates a field as well as reopens
            ThreadClass[] threads = new ThreadClass[numThreads];
            for (int i = 0; i < threads.Length; i++)
            {
                string f  = "f" + i;
                string cf = "cf" + i;
                threads[i] = new ThreadAnonymousInnerClassHelper(this, "UpdateThread-" + i, writer, numDocs, done, numUpdates, f, cf);
            }

            foreach (ThreadClass t in threads)
            {
                t.Start();
            }
            done.Wait();
            writer.Dispose();

            DirectoryReader reader  = DirectoryReader.Open(dir);
            BytesRef        scratch = new BytesRef();

            foreach (AtomicReaderContext context in reader.Leaves)
            {
                AtomicReader r = context.AtomicReader;
                for (int i = 0; i < numThreads; i++)
                {
                    BinaryDocValues  bdv             = r.GetBinaryDocValues("f" + i);
                    NumericDocValues control         = r.GetNumericDocValues("cf" + i);
                    IBits            docsWithBdv     = r.GetDocsWithField("f" + i);
                    IBits            docsWithControl = r.GetDocsWithField("cf" + i);
                    IBits            liveDocs        = r.LiveDocs;
                    for (int j = 0; j < r.MaxDoc; j++)
                    {
                        if (liveDocs == null || liveDocs.Get(j))
                        {
                            Assert.AreEqual(docsWithBdv.Get(j), docsWithControl.Get(j));
                            if (docsWithBdv.Get(j))
                            {
                                long ctrlValue = control.Get(j);
                                long bdvValue  = TestBinaryDocValuesUpdates.GetValue(bdv, j, scratch) * 2;
                                //              if (ctrlValue != bdvValue) {
                                //                System.out.println("seg=" + r + ", f=f" + i + ", doc=" + j + ", group=" + r.Document(j).Get("updKey") + ", ctrlValue=" + ctrlValue + ", bdvBytes=" + scratch);
                                //              }
                                Assert.AreEqual(ctrlValue, bdvValue);
                            }
                        }
                    }
                }
            }
            reader.Dispose();

            dir.Dispose();
        }
Example #25
0
            //---------------------------------------------------------------------------------------
            // Straightforward IEnumerator<T> methods.
            //

            internal override bool MoveNext([MaybeNullWhen(false), AllowNull] ref TResult currentElement, ref TKey currentKey)
            {
                // If the buffer has not been created, we will generate it lazily on demand.
                if (_buffer == null)
                {
                    // Create a buffer, but don't publish it yet (in case of exception).
                    List <Pair <TResult, TKey> > buffer = new List <Pair <TResult, TKey> >();

                    // Enter the search phase.  In this phase, we scan the input until one of three
                    // things happens:  (1) all input has been exhausted, (2) the predicate yields
                    // false for one of our elements, or (3) we move past the current lowest index
                    // found by other partitions for a false element.  As we go, we have to remember
                    // the elements by placing them into the buffer.

                    try
                    {
                        TResult current = default(TResult) !;
                        TKey    key     = default(TKey) !;
                        int     i       = 0; //counter to help with cancellation
                        while (_source.MoveNext(ref current !, ref key))
                        {
                            if ((i++ & CancellationState.POLL_INTERVAL) == 0)
                            {
                                _cancellationToken.ThrowIfCancellationRequested();
                            }
                            ;

                            // Add the current element to our buffer.
                            buffer.Add(new Pair <TResult, TKey>(current, key));

                            // See if another partition has found a false value before this element. If so,
                            // we should stop scanning the input now and reach the barrier ASAP.
                            if (_updatesSeen != _operatorState._updatesDone)
                            {
                                lock (_operatorState)
                                {
                                    _currentLowKey = _operatorState._currentLowKey;
                                    _updatesSeen   = _operatorState._updatesDone;
                                }
                            }

                            if (_updatesSeen > 0 && _keyComparer.Compare(key, _currentLowKey) > 0)
                            {
                                break;
                            }

                            // Evaluate the predicate, either indexed or not based on info passed to the ctor.
                            bool predicateResult;
                            if (_predicate != null)
                            {
                                predicateResult = _predicate(current);
                            }
                            else
                            {
                                Debug.Assert(_indexedPredicate != null);
                                predicateResult = _indexedPredicate(current, key);
                            }

                            if (!predicateResult)
                            {
                                // Signal that we've found a false element, racing with other partitions to
                                // set the shared index value.
                                lock (_operatorState)
                                {
                                    if (_operatorState._updatesDone == 0 || _keyComparer.Compare(_operatorState._currentLowKey, key) > 0)
                                    {
                                        _currentLowKey = _operatorState._currentLowKey = key;
                                        _updatesSeen   = ++_operatorState._updatesDone;
                                    }
                                }

                                break;
                            }
                        }
                    }
                    finally
                    {
                        // No matter whether we exit due to an exception or normal completion, we must ensure
                        // that we signal other partitions that we have completed.  Otherwise, we can cause deadlocks.
                        _sharedBarrier.Signal();
                    }

                    // Before exiting the search phase, we will synchronize with others. This is a barrier.
                    _sharedBarrier.Wait(_cancellationToken);

                    // Publish the buffer and set the index to just before the 1st element.
                    _buffer      = buffer;
                    _bufferIndex = new Shared <int>(-1);
                }

                Debug.Assert(_bufferIndex != null);
                // Now either enter (or continue) the yielding phase. As soon as we reach this, we know the
                // current shared "low false" value is the absolute lowest with a false.
                if (_take)
                {
                    // In the case of a take-while, we will yield each element from our buffer for which
                    // the element is lesser than the lowest false index found.
                    if (_bufferIndex.Value >= _buffer.Count - 1)
                    {
                        return(false);
                    }

                    // Increment the index, and remember the values.
                    ++_bufferIndex.Value;
                    currentElement = _buffer[_bufferIndex.Value].First;
                    currentKey     = _buffer[_bufferIndex.Value].Second;

                    return(_operatorState._updatesDone == 0 || _keyComparer.Compare(_operatorState._currentLowKey, currentKey) > 0);
                }
                else
                {
                    // If no false was found, the output is empty.
                    if (_operatorState._updatesDone == 0)
                    {
                        return(false);
                    }

                    // In the case of a skip-while, we must skip over elements whose index is lesser than the
                    // lowest index found. Once we've exhausted the buffer, we must go back and continue
                    // enumerating the data source until it is empty.
                    if (_bufferIndex.Value < _buffer.Count - 1)
                    {
                        for (_bufferIndex.Value++; _bufferIndex.Value < _buffer.Count; _bufferIndex.Value++)
                        {
                            // If the current buffered element's index is greater than or equal to the smallest
                            // false index found, we will yield it as a result.
                            if (_keyComparer.Compare(_buffer[_bufferIndex.Value].Second, _operatorState._currentLowKey) >= 0)
                            {
                                currentElement = _buffer[_bufferIndex.Value].First;
                                currentKey     = _buffer[_bufferIndex.Value].Second;
                                return(true);
                            }
                        }
                    }

                    // Lastly, so long as our input still has elements, they will be yieldable.
                    if (_source.MoveNext(ref currentElement !, ref currentKey))
                    {
                        Debug.Assert(_keyComparer.Compare(currentKey, _operatorState._currentLowKey) > 0,
                                     "expected remaining element indices to be greater than smallest");
                        return(true);
                    }
                }

                return(false);
            }
Example #26
0
        public unsafe static bool SaveImageBytes(byte[][,] rgb, string fileName, string format, int threadsToRun = 0)
        {
            if (3 != rgb.GetLength(0))
            {
                throw new ArrayTypeMismatchException("Size of first dimension for passed array must be 3 (RGB components).");
            }

            if (2 != rgb[0].Rank)
            {
                throw new ArrayTypeMismatchException("Every passed component array should have 2 dimensions.");
            }

            int        height = rgb[0].GetLength(0), width = rgb[0].GetLength(1);
            Bitmap     res = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            BitmapData bd  = res.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

            try {
                if (2 > threadsToRun)
                {
                    int   h, w;
                    byte *curpos;

                    Console.WriteLine("Using 1 thread to save image");

                    for (h = 0; h < height; h++)
                    {
                        curpos = (( byte * )bd.Scan0) + h * bd.Stride;
                        for (w = 0; w < width; w++)
                        {
                            *(curpos++) = rgb[2][h, w];
                            *(curpos++) = rgb[1][h, w];
                            *(curpos++) = rgb[0][h, w];
                        }
                    }
                }
                else
                {
                    int step = height / threadsToRun, rest = height % threadsToRun;

                    Console.WriteLine("Using " + threadsToRun + " threads to save image");

                    using (CountdownEvent completed = new CountdownEvent(threadsToRun)) {
                        for (int num = 0; num < threadsToRun; num++)
                        {
                            ThreadPool.QueueUserWorkItem(new WaitCallback(idx => {
                                int i     = ( int )idx;
                                int start = i * step, finish = start + step, h, w;
                                byte *curpos;

                                if (i < rest)
                                {
                                    start  += i;
                                    finish += i + 1;
                                }
                                else
                                {
                                    start  += rest;
                                    finish += rest;
                                }

                                Console.WriteLine("Thread " + i + " saving lines from " + start + " till " + finish);

                                for (h = start; h < finish; h++)
                                {
                                    curpos = (( byte * )bd.Scan0) + h * bd.Stride;
                                    for (w = 0; w < width; w++)
                                    {
                                        *(curpos++) = rgb[2][h, w];
                                        *(curpos++) = rgb[1][h, w];
                                        *(curpos++) = rgb[0][h, w];
                                    }
                                }

                                completed.Signal();
                            }), num);
                        }

                        completed.Wait();
                    }
                }
            } finally {
                res.UnlockBits(bd);
            }

            return(SaveBitmap(ref res, fileName, Ext2Fmt(format)));
        }
Example #27
0
        public static void Main()
        {
            using (var ignite = Ignition.StartFromApplicationConfiguration())
            {
                var remotes = ignite.GetCluster().ForRemotes();

                if (remotes.GetNodes().Count == 0)
                {
                    Console.WriteLine(">>> This example requires remote nodes to be started.");
                    Console.WriteLine(">>> Please start at least 1 remote node.");
                    Console.WriteLine(">>> Refer to example's documentation for details on configuration.");
                }
                else
                {
                    Console.WriteLine(">>> Messaging example started.");
                    Console.WriteLine();

                    // Set up local listeners
                    var localMessaging = ignite.GetCluster().ForLocal().GetMessaging();

                    var msgCount = remotes.GetNodes().Count * 10;

                    var orderedCounter   = new CountdownEvent(msgCount);
                    var unorderedCounter = new CountdownEvent(msgCount);

                    localMessaging.LocalListen(new LocalListener(unorderedCounter), Topic.Unordered);

                    localMessaging.LocalListen(new LocalListener(orderedCounter), Topic.Ordered);

                    // Set up remote listeners
                    var remoteMessaging = remotes.GetMessaging();

                    var idUnordered = remoteMessaging.RemoteListen(new RemoteUnorderedListener(), Topic.Unordered);
                    var idOrdered   = remoteMessaging.RemoteListen(new RemoteOrderedListener(), Topic.Ordered);

                    // Send unordered
                    Console.WriteLine(">>> Sending unordered messages...");

                    for (var i = 0; i < 10; i++)
                    {
                        remoteMessaging.Send(i, Topic.Unordered);
                    }

                    Console.WriteLine(">>> Finished sending unordered messages.");

                    // Send ordered
                    Console.WriteLine(">>> Sending ordered messages...");

                    for (var i = 0; i < 10; i++)
                    {
                        remoteMessaging.SendOrdered(i, Topic.Ordered);
                    }

                    Console.WriteLine(">>> Finished sending ordered messages.");

                    Console.WriteLine(">>> Check output on all nodes for message printouts.");
                    Console.WriteLine(">>> Waiting for messages acknowledgements from all remote nodes...");

                    unorderedCounter.Wait();
                    orderedCounter.Wait();

                    // Unsubscribe
                    remoteMessaging.StopRemoteListen(idUnordered);
                    remoteMessaging.StopRemoteListen(idOrdered);
                }
            }

            Console.WriteLine();
            Console.WriteLine(">>> Example finished, press any key to exit ...");
            Console.ReadKey();
        }
Example #28
0
        public unsafe static byte[][,] LoadImageBytes(string fileName, int threadsToRun = 0)
        {
            Bitmap bmp = LoadBitmap(fileName);

            if (null == bmp)
            {
                return(null);
            }

            int width = bmp.Width, height = bmp.Height;

            byte[][,] res = CreateImageBytes(height, width);
            BitmapData bd = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            try {
                if (2 > threadsToRun)
                {
                    int   h, w;
                    byte *curpos;

                    Console.WriteLine("Using 1 thread to load image");

                    for (h = 0; h < height; h++)
                    {
                        curpos = (( byte * )bd.Scan0) + h * bd.Stride;
                        for (w = 0; w < width; w++)
                        {
                            res[2][h, w] = *(curpos++);
                            res[1][h, w] = *(curpos++);
                            res[0][h, w] = *(curpos++);
                        }
                    }
                }
                else
                {
                    int step = height / threadsToRun, rest = height % threadsToRun;

                    Console.WriteLine("Using " + threadsToRun + " threads to load image");

                    using (CountdownEvent completed = new CountdownEvent(threadsToRun)) {
                        for (int num = 0; num < threadsToRun; num++)
                        {
                            ThreadPool.QueueUserWorkItem(new WaitCallback(idx => {
                                int i     = ( int )idx;
                                int start = i * step, finish = start + step, h, w;
                                byte *curpos;

                                if (i < rest)
                                {
                                    start  += i;
                                    finish += i + 1;
                                }
                                else
                                {
                                    start  += rest;
                                    finish += rest;
                                }

                                Console.WriteLine("Thread " + i + " loading lines from " + start + " till " + finish);

                                for (h = start; h < finish; h++)
                                {
                                    curpos = (( byte * )bd.Scan0) + h * bd.Stride;
                                    for (w = 0; w < width; w++)
                                    {
                                        res[2][h, w] = *(curpos++);
                                        res[1][h, w] = *(curpos++);
                                        res[0][h, w] = *(curpos++);
                                    }
                                }

                                completed.Signal();
                            }), num);
                        }

                        completed.Wait();
                    }
                }
            } finally {
                bmp.UnlockBits(bd);
            }

            return(res);
        }
Example #29
0
        public void MultiOffer2()
        {
            var q = new MpscLinkedArrayQueue <int>(4);

            int[] sync = { 2 };

            CountdownEvent latch = new CountdownEvent(2);

            Task.Factory.StartNew(() =>
            {
                Interlocked.Decrement(ref sync[0]);
                while (Volatile.Read(ref sync[0]) != 0)
                {
                    ;
                }

                for (int i = 0; i < 500000; i++)
                {
                    if (i % 50000 == 0)
                    {
                        Console.WriteLine("Written " + i);
                    }
                    q.Offer(i);
                }
                Console.WriteLine("Written " + 500000);
                latch.Signal();
            }, TaskCreationOptions.LongRunning);

            Task.Factory.StartNew(() =>
            {
                Interlocked.Decrement(ref sync[0]);
                while (Volatile.Read(ref sync[0]) != 0)
                {
                    ;
                }

                for (int i = 500000; i < 1000000; i++)
                {
                    if (i % 50000 == 0)
                    {
                        Console.WriteLine("Written " + i);
                    }
                    q.Offer(i);
                }
                Console.WriteLine("Written " + 1000000);
                latch.Signal();
            }, TaskCreationOptions.LongRunning);

            latch.Wait(5000);

            for (int i = 0; i < 1000000; i++)
            {
                if (i % 10000 == 0)
                {
                    Console.WriteLine("Read " + i);
                }
                if (!q.Poll(out int item))
                {
                    Assert.Fail("Queue appears to be empty?!");
                }
            }

            Assert.IsTrue(q.IsEmpty());
        }
Example #30
0
        public static void RunWithProcessorPressure(int threadCount, int lockCount, double activePercent, double lockChancePercent, Action a)
        {
            var stopped = 0;
            var started = new CountdownEvent(threadCount);

            var ts    = new Thread[threadCount];
            var locks = Enumerable.Range(0, lockCount).Select(_ => new object()).ToArray();

            for (int i = 0; i < threadCount; i++)
            {
                var id = i;

                var t = ts[i] = new Thread(() =>
                {
                    var rand = new Random();

                    started.Signal();

                    var sw  = Stopwatch.StartNew();
                    var run = TimeSpan.Zero;

                    while (Thread.VolatileRead(ref stopped) == 0)
                    {
                        var busy = new Stopwatch();

                        while (Thread.VolatileRead(ref stopped) == 0 && (double)run.Ticks / (double)sw.ElapsedTicks < activePercent)
                        {
                            busy.Restart();

                            const int RUN   = 0;
                            const int BLOCK = 1;

                            var action = lockCount > 0 && rand.Next() % 100 <= lockChancePercent * 100 ? BLOCK : RUN;

                            switch (action)
                            {
                            case RUN:
                                //Console.WriteLine("~" + id);
                                while (busy.ElapsedMilliseconds < 10)
                                {
                                    ;
                                }
                                break;

                            case BLOCK:
                                //Console.WriteLine("!" + id);
                                lock (locks[rand.Next(0, lockCount)])
                                    Thread.Sleep(rand.Next(100, 1000));
                                break;
                            }

                            run += busy.Elapsed;
                        }

                        Thread.Sleep(rand.Next(100, 1000));
                    }
                });

                t.Start();
            }

            started.Wait();

            try
            {
                a();
            }
            finally
            {
                Interlocked.Exchange(ref stopped, 1);
                foreach (var t in ts)
                {
                    t.Join();
                }
            }
        }
Example #31
0
        public void TestSemaphoreRejected()
        {
            IHystrixCommandGroupKey groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-H");
            IHystrixThreadPoolKey   threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-H");
            IHystrixCommandKey      key           = HystrixCommandKeyDefault.AsKey("RollingCounter-H");

            stream = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            stream.StartCachingStreamValuesIfUnstarted();

            CountdownEvent latch = new CountdownEvent(1);

            stream.Observe().Take(5).Subscribe(GetSubscriber(output, latch));

            // 10 commands will saturate semaphore when called from different threads.
            // submit 2 more requests and they should be SEMAPHORE_REJECTED
            // should see 10 SUCCESSes, 2 SEMAPHORE_REJECTED and 2 FALLBACK_SUCCESSes
            List <Command> saturators = new List <Command>();

            for (int i = 0; i < 10; i++)
            {
                saturators.Add(CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 500, ExecutionIsolationStrategy.SEMAPHORE));
            }

            CommandStreamTest.Command rejected1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 0, ExecutionIsolationStrategy.SEMAPHORE);
            CommandStreamTest.Command rejected2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 0, ExecutionIsolationStrategy.SEMAPHORE);

            foreach (CommandStreamTest.Command saturator in saturators)
            {
                Task t = new Task(
                    () =>
                {
                    saturator.Observe();
                }, CancellationToken.None,
                    TaskCreationOptions.LongRunning);
                t.Start();
            }

            try
            {
                Time.Wait(100);
            }
            catch (Exception ie)
            {
                Assert.False(true, ie.Message);
            }

            rejected1.Observe();
            rejected2.Observe();

            try
            {
                Assert.True(latch.Wait(10000));
            }
            catch (Exception)
            {
                Assert.False(true, "Interrupted ex");
            }

            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());
            Assert.True(rejected1.IsResponseSemaphoreRejected);
            Assert.True(rejected2.IsResponseSemaphoreRejected);

            // none of these got executed on a thread-pool, so thread pool metrics should be 0
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
Example #32
0
        private bool WaitForBatchAllowingPartialBatchResumption(CountdownEvent completionEvent, BatchStatistics batch, List <ThreadTask> childTasks, DocumentDatabase database = null)
        {
            Interlocked.Increment(ref _hasPartialBatchResumption);
            var cancellationToken = database?.WorkContext.CancellationToken ?? _ct;

            try
            {
                var waitHandles            = new[] { completionEvent.WaitHandle, _threadHasNoWorkToDo };
                var sp                     = Stopwatch.StartNew();
                var batchRanToCompletion   = false;
                var lastThreadIndexChecked = 0;
                while (cancellationToken.IsCancellationRequested == false)
                {
                    // First, try to find work to do among child tasks, instead of just waiting
                    ThreadTask busyWaitWorkToDo = null;
                    for (; lastThreadIndexChecked < childTasks.Count && completionEvent.IsSet == false && _ct.IsCancellationRequested == false; lastThreadIndexChecked++)
                    {
                        var task = childTasks[lastThreadIndexChecked];
                        if (Interlocked.CompareExchange(ref task.Proccessing, 1, 0) == 1)
                        {
                            continue;
                        }
                        busyWaitWorkToDo = task;
                        break;
                    }

                    int returnedWaitHandleIndex;

                    // Run found work or just wait for all work to finish (returnedWaitHandleIndex=0) or to be notified about free threads in the system (returnedWaitHandleIndex=1)
                    // After which we'll decide whether we should early exit, or to wait some more
                    if (busyWaitWorkToDo != null)
                    {
                        RunThreadTask(busyWaitWorkToDo);
                        returnedWaitHandleIndex = WaitHandle.WaitAny(waitHandles, 0);
                    }
                    else
                    {
                        returnedWaitHandleIndex = WaitHandle.WaitAny(waitHandles);
                    }

                    if (returnedWaitHandleIndex == 0)
                    {
                        break;
                    }

                    // we won't consider breaking early if we haven't completed at least half the work
                    if (Thread.VolatileRead(ref batch.Completed) < batch.Total / 2)
                    {
                        continue;
                    }

                    var currentFreeThreads = _freedThreadsValue.Values.Count(isFree => isFree);

                    // we will break early only if there are more then half free threads
                    if (currentFreeThreads > _currentWorkingThreadsAmount / 2)
                    {
                        break;
                    }
                }

                // after we've decided to quit early, we will wait some more time, allowing a normal wait.
                // we decide how much we will wait by choosing the biggest among the next:
                // 1) half the time we've waited on the current batch
                // 2) a waiting time factor that increase for every second left early batch and decreases for every leave early batch that completed before leaving early

                var elapsedMilliseconds = (int)sp.ElapsedMilliseconds;
                if (completionEvent.Wait(Math.Max(elapsedMilliseconds / 2, Thread.VolatileRead(ref _partialMaxWait)), cancellationToken))
                {
                    _concurrentEvents.Enqueue(completionEvent);
                }

                // updating the waiting time factor
                if (batch.Completed != batch.Total)
                {
                    if (_partialMaxWaitChangeFlag > 0)
                    {
                        Interlocked.Exchange(ref _partialMaxWait, Math.Min(2500, (int)(Thread.VolatileRead(ref _partialMaxWait) * 1.25)));
                    }
                }
                else if (_partialMaxWaitChangeFlag < 0)
                {
                    batchRanToCompletion = true;
                    Interlocked.Exchange(ref _partialMaxWait, Math.Max(Thread.VolatileRead(ref _partialMaxWait) / 2, 10));
                }

                Interlocked.Exchange(ref _partialMaxWaitChangeFlag, Thread.VolatileRead(ref _partialMaxWaitChangeFlag) * -1);

                // completionEvent is explicitly left to the finalizer
                // we expect this to be rare, and it isn't worth the trouble of trying
                // to manage this

                if (logger.IsDebugEnabled)
                {
                    logger.Debug($"Raven Thread Pool ended batch for database {database?.Name}. Done {batch.Completed} items out of {batch.Total}. Batch {(batchRanToCompletion ? "was not" : "was")} left early");
                }

                return(batchRanToCompletion);
            }
            finally
            {
                Interlocked.Decrement(ref _hasPartialBatchResumption);
            }
        }
Example #33
0
 public void OnStart()
 {
     _globalSignal.Signal();
     _globalSignal.Wait();
 }
 public bool @await()
 {
     return(waiter.Wait(new TimeSpan(0, 0, 0, 10)));
 }
Example #35
0
 public void Third(Action printThird)
 {
     second.Wait();
     // printThird() outputs "third". Do not change or remove this line.
     printThird();
 }
        public void TestTwoSubscribersBothUnsubscribe()
        {
            CountdownEvent latch1    = new CountdownEvent(1);
            CountdownEvent latch2    = new CountdownEvent(1);
            AtomicInteger  payloads1 = new AtomicInteger(0);
            AtomicInteger  payloads2 = new AtomicInteger(0);

            IDisposable s1 = stream
                             .Observe()
                             .Take(100)
                             .OnDispose(() =>
            {
                latch1.SignalEx();
            })
                             .Subscribe(
                (utilization) =>
            {
                output.WriteLine((DateTime.Now.Ticks / 10000) + " : " + Thread.CurrentThread.ManagedThreadId + " : Dashboard 1 OnNext : " + utilization);
                payloads1.IncrementAndGet();
            },
                (e) =>
            {
                output.WriteLine((DateTime.Now.Ticks / 10000) + " : " + Thread.CurrentThread.ManagedThreadId + " Dashboard 1 OnError : " + e);
                latch1.SignalEx();
            },
                () =>
            {
                output.WriteLine((DateTime.Now.Ticks / 10000) + " : " + Thread.CurrentThread.ManagedThreadId + " Dashboard 1 OnCompleted");
                latch1.SignalEx();
            });

            IDisposable s2 = stream
                             .Observe()
                             .Take(100)
                             .OnDispose(() =>
            {
                latch2.SignalEx();
            })
                             .Subscribe(
                (utilization) =>
            {
                output.WriteLine((DateTime.Now.Ticks / 10000) + " : " + Thread.CurrentThread.ManagedThreadId + " : Dashboard 2 OnNext : " + utilization);
                payloads2.IncrementAndGet();
            },
                (e) =>
            {
                output.WriteLine((DateTime.Now.Ticks / 10000) + " : " + Thread.CurrentThread.ManagedThreadId + " Dashboard 2 OnError : " + e);
                latch2.SignalEx();
            },
                () =>
            {
                output.WriteLine((DateTime.Now.Ticks / 10000) + " : " + Thread.CurrentThread.ManagedThreadId + " Dashboard 2 OnCompleted");
                latch2.SignalEx();
            });

            // execute 2 commands, then unsubscribe from both streams, then execute the rest
            for (int i = 0; i < 10; i++)
            {
                HystrixCommand <int> cmd = Command.From(GroupKey, CommandKey, HystrixEventType.SUCCESS, 50);
                cmd.Execute();
                if (i == 2)
                {
                    s1.Dispose();
                    s2.Dispose();
                }
            }

            Assert.False(stream.IsSourceCurrentlySubscribed);  // both subscriptions have been cancelled - source should be too

            Assert.True(latch1.Wait(10000));
            Assert.True(latch2.Wait(10000));
            output.WriteLine("s1 got : " + payloads1.Value + ", s2 got : " + payloads2.Value);
            Assert.True(payloads1.Value > 0); // "s1 got data",
            Assert.True(payloads2.Value > 0); // "s2 got data",
        }
Example #37
0
        /// <summary>
        /// 百度矢量要素下载(数据下载写入shp文件的时候可能出现问题,比如下载网络波动中断到时系统无法识别(解决办法先吧数据下载到数据在写入shp文件))
        /// </summary>
        /// <param name="Result"></param>
        /// <returns></returns>
        public WebApiResult <string> BaiduDownload(BaiduMapFeatureDownloadResult Result)
        {
            this.countdown       = new CountdownEvent(Result.TaskCount);
            this.Result_BFeature = Result;
            this.N = Result.TaskCount;

            UpdateLastLoc <BaiduMapFeatureDownloadResult>(Result.GUID);

            log = new TaskLogEntity()
            {
                GUID = Result.GUID, Name = Result.TName, Type = "矢量要素", Description = "BaiduDownload", Status = "进行中", Parameter = JsonHelper.ToJson(Result), SavePath = Result.SavePathText
            };
            //操作日志
            new Log <BaiduMapFeatureDownloadResult>(log);

            if (Result.LayerName == "道路")
            {
                xmlDoc.Load(MapFeatureConfig);

                ///////////////////////////////////////////////////////////////////////////////
                // GDAL创建shp文件
                // 注册所有的驱动
                Ogr.RegisterAll();
                // GDAL中文路径支持
                OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
                // 属性表字段中文支持
                OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "CP936");
                // 设置坐标系存储文件夹gdal-data路径
                OSGeo.GDAL.Gdal.SetConfigOption("GDAL_DATA", System.Windows.Forms.Application.StartupPath + "\\gdal-data");
                Driver oDriver = Ogr.GetDriverByName("ESRI Shapefile");
                if (oDriver == null)
                {
                    return(new WebApiResult <string>()
                    {
                        success = 0, msg = "GDAL驱动不可用,请检查!"
                    });
                }
                // 创建数据源
                DataSource oDS = oDriver.CreateDataSource(Result.SavePathText, null);
                if (oDS == null)
                {
                    return(new WebApiResult <string>()
                    {
                        success = 0, msg = "创建数据源失败,请检查!"
                    });
                }
                oLayer = oDS.GetLayerByName(Result.TName);
                if (oLayer == null)
                {
                    // 创建图层,创建一个多线图层,并指定空间参考
                    OSGeo.OSR.SpatialReference oSRS = new OSGeo.OSR.SpatialReference("PROJCS[\"WGS_1984_Pseudo_Mercator\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Mercator\"],PARAMETER[\"false_easting\",0.0],PARAMETER[\"false_northing\",0.0],PARAMETER[\"central_meridian\",0.0],PARAMETER[\"standard_parallel_1\",0.0],UNIT[\"Meter\",1.0]]");
                    //OSGeo.OSR.SpatialReference oSRS = new OSGeo.OSR.SpatialReference("");
                    //oSRS.SetWellKnownGeogCS("EPSG:4326");
                    oLayer = oDS.CreateLayer(Result.TName, oSRS, wkbGeometryType.wkbMultiLineString, null);
                    if (oLayer == null)
                    {
                        return(new WebApiResult <string>()
                        {
                            success = 0, msg = "创建图层失败,请检查!"
                        });
                    }

                    // 创建属性表
                    // 先创建一个叫Name的字符型属性,字符长度为100
                    FieldDefn oFieldName = new FieldDefn("Name", FieldType.OFTString);
                    oFieldName.SetWidth(100);
                    oLayer.CreateField(oFieldName, 1);
                    // 再创建一个叫Type的字符型属性,字符长度为50
                    FieldDefn oFieldType = new FieldDefn("Type", FieldType.OFTString);
                    oFieldType.SetWidth(50);
                    oLayer.CreateField(oFieldType, 1);
                    // 再创建一个叫UID的字符型属性,字符长度为50
                    FieldDefn oFieldUID = new FieldDefn("UID", FieldType.OFTString);
                    oFieldUID.SetWidth(50);
                    oLayer.CreateField(oFieldUID, 1);
                }

                /////////////////////////////////////////////////////////////////////////////////
                threadlog = new ThreadTaskLogEntity()
                {
                    GUID = Result.GUID, TaskLog_GUID = Result.GUID, Status = "进行中", TStatus = 1, TName = Result.TName, IsPaused = false, URL = Result.URL, Parameter = JsonHelper.ToJson(Result)
                };
                //计算Total并更新
                threadlog.Total = GetList("SELECT KWName FROM mapbar_poi WHERE KWType = '道路'", "KWName").Count;
                new Log <BaiduMapFeatureDownloadResult>(threadlog);
                log.Count = threadlog.Total;
                new Log <BaiduMapFeatureDownloadResult>(log);

                Thread[] t = new Thread[Result.TaskCount];
                for (int num = 0; num < Result.TaskCount; num++)
                {
                    try
                    {
                        t[num] = new Thread(new ParameterizedThreadStart(run_BFeature))
                        {
                            Name = "Thread " + num.ToString()
                        };
                        t[num].Start(num);
                    }
                    catch (Exception ex)
                    {
                        threadlog.Status   = "错误";
                        threadlog.ErrorMsg = ex.ToString();
                        threadlog.TStatus  = 3;
                        new Log <BaiduMapFeatureDownloadResult>(threadlog);

                        log.Status    = "错误";
                        log.ErrorMsg  = ex.ToString();
                        log.ErrorDate = DateTime.Now.ToString();
                        //操作日志
                        new Log <BaiduMapFeatureDownloadResult>(log);

                        return(new WebApiResult <string>()
                        {
                            success = 0, msg = ex.ToString()
                        });
                    }
                }
                countdown.Wait();
                oLayer.Dispose();
                oDS.Dispose();
                for (int num = 0; num < Result.TaskCount; num++)
                {
                    t[num].Abort();
                }
                lock (obj)
                {
                    if (!Log <BaiduMapFeatureDownloadResult> .GetThreadLogEntity(this.Result_BFeature.GUID).IsPaused)
                    {
                        //配置文件参数信息更新
                        xmlDoc["MapFeature"]["bmap"]["dict_index"].InnerText = "0";
                        xmlDoc.Save(MapFeatureConfig);

                        log.Status            = "已完成";
                        log.CompleteTime      = DateTime.Now.ToString();
                        log.Current           = log.Count;
                        threadlog.Status      = "已完成";
                        threadlog.TStatus     = 2;
                        threadlog.Current     = threadlog.Total;
                        threadlog.Current_loc = List2Str(current_loc);
                        //操作日志
                        new Log <BaiduMapFeatureDownloadResult>(threadlog);
                        new Log <BaiduMapFeatureDownloadResult>(log);
                        return(new WebApiResult <string>()
                        {
                            success = 1, msg = "百度地图要素下载完成!"
                        });
                    }
                }
            }
            else
            {
                return(new WebApiResult <string>()
                {
                    success = 2, msg = "图层类型不符合"
                });
            }
            return(null);
        }
        public void TestTwoSubscribersOneUnsubscribes()
        {
            CountdownEvent latch1    = new CountdownEvent(1);
            CountdownEvent latch2    = new CountdownEvent(1);
            AtomicInteger  payloads1 = new AtomicInteger(0);
            AtomicInteger  payloads2 = new AtomicInteger(0);

            IDisposable s1 = stream
                             .Observe()
                             .Take(100)
                             .OnDispose(() =>
            {
                latch1.SignalEx();
            })
                             .Subscribe(
                (utilization) =>
            {
                output.WriteLine((DateTime.Now.Ticks / 10000) + " : " + Thread.CurrentThread.ManagedThreadId + " : Dashboard 1 OnNext : " + utilization);
                payloads1.IncrementAndGet();
            },
                (e) =>
            {
                output.WriteLine((DateTime.Now.Ticks / 10000) + " : " + Thread.CurrentThread.ManagedThreadId + " Dashboard 1 OnError : " + e);
                latch1.SignalEx();
            },
                () =>
            {
                output.WriteLine((DateTime.Now.Ticks / 10000) + " : " + Thread.CurrentThread.ManagedThreadId + " Dashboard 1 OnCompleted");
                latch1.SignalEx();
            });

            IDisposable s2 = stream
                             .Observe()
                             .Take(100)
                             .OnDispose(() =>
            {
                latch2.SignalEx();
            })
                             .Subscribe(
                (utilization) =>
            {
                output.WriteLine((DateTime.Now.Ticks / 10000) + " : " + Thread.CurrentThread.ManagedThreadId + " : Dashboard 2 OnNext : " + utilization);
                payloads2.IncrementAndGet();
            },
                (e) =>
            {
                output.WriteLine((DateTime.Now.Ticks / 10000) + " : " + Thread.CurrentThread.ManagedThreadId + " Dashboard 2 OnError : " + e);
                latch2.SignalEx();
            },
                () =>
            {
                output.WriteLine((DateTime.Now.Ticks / 10000) + " : " + Thread.CurrentThread.ManagedThreadId + " Dashboard 2 OnCompleted");
                latch2.SignalEx();
            });

            // execute 1 command, then unsubscribe from first stream. then execute the rest
            for (int i = 0; i < 50; i++)
            {
                HystrixCommand <int> cmd = Command.From(GroupKey, CommandKey, HystrixEventType.SUCCESS, 50);
                cmd.Execute();
                if (i == 1)
                {
                    s1.Dispose();
                }
            }

            Assert.True(latch1.Wait(10000));
            Assert.True(latch2.Wait(10000));
            output.WriteLine("s1 got : " + payloads1.Value + ", s2 got : " + payloads2.Value);
            Assert.True(payloads1.Value > 0);               // "s1 got data",
            Assert.True(payloads2.Value > 0);               // "s2 got data",
            Assert.True(payloads2.Value > payloads1.Value); // "s1 got less data than s2",
        }
        /// <summary>
        /// Calculates Node based Betweeness Centrality using multiple threads
        /// </summary>
        /// <param name="g"></param>
        /// <returns></returns>
        public static double[] ParallelBrandesBcNodes(LightWeightGraph g)
        {
            int numNodes      = g.NumNodes;
            int numThreads    = Settings.Threading.NumThreadsBc;
            int workSize      = numNodes / numThreads;
            int workSizeExtra = numNodes % numThreads;

            //Start getting a randomized work load
            List <int> nodes = new List <int>(numNodes);

            for (int i = 0; i < numNodes; i++)
            {
                nodes.Add(i);
            }
            nodes.Shuffle();

            //Create an array of work items for each thread and assign the nodes in a randomized order
            int[][] workItems = new int[numThreads][];
            for (int t = 0; t < numThreads; t++)
            {
                int size = workSize + (t == (numThreads - 1) ? workSizeExtra : 0);
                workItems[t] = new int[size];
                for (int i = 0; i < size; i++)
                {
                    workItems[t][i] = nodes[t * workSize + i];
                }
            }

            //Create our threads use a closure to get our return arrays
            BetweenessCalc[] threadResults = new BetweenessCalc[numThreads];
            //ManualResetEvent[] waitHandles = new ManualResetEvent[numThreads];
            CountdownEvent cde = new CountdownEvent(numThreads);

            for (int t = 0; t < numThreads; t++)
            {
                int tIndex = t;
                //waitHandles[tIndex] = new ManualResetEvent(false);
                BetweenessCalc tResult = new BetweenessCalc(g, workItems[tIndex]);
                threadResults[tIndex] = tResult;
                ThreadPool.QueueUserWorkItem(tResult.ThreadPoolCallback, cde);
            }
            cde.Wait();
            //WaitHandle.WaitAll(waitHandles);

            //Create our betweeness map and sum all of the thread results
            double[] bcMap = new double[numNodes];
            for (int t = 0; t < numThreads; t++)
            {
                var threadR = threadResults[t].BcMap;
                for (int n = 0; n < numNodes; n++)
                {
                    bcMap[n] += threadR[n];
                }
            }

            //divide all by 2 (undirected)
            for (int v = 0; v < numNodes; v++)
            {
                bcMap[v] /= 2f;
            }

            return(bcMap);
        }
        public void ClosedSocketFromServer()
        {
            int    port   = 15001;
            string data   = "testData";
            bool   closed = false;

            EasyServer server = new EasyServer();

            Task.Run(() =>
            {
                server.ConnectHandler(socket =>
                {
                    string socketId = socket.SocketId;
                    socket.CloseHandler(() =>
                    {
                        _output.WriteLine($"[{socketId}] server socket close handler");
                    });
                    socket.ExceptionHandler(exception =>
                    {
                        _output.WriteLine($"[{socketId}] server socket exception handler - {exception}");
                    });
                    socket.Receive(receivedData =>
                    {
                        string stringData = Encoding.UTF8.GetString(receivedData);
                        _output.WriteLine($"[{socketId}] server socket receive - {stringData}:{stringData.Length}");
                        socket.Close();
                    });
                });
                server.ExceptionHandler(exception =>
                {
                    _output.WriteLine($"server start exception handler - {exception}");
                });
                server.Start("127.0.0.1", port);
            }).Wait(1500);

            EasyClient     client         = new EasyClient();
            CountdownEvent countdownEvent = new CountdownEvent(1);

            client.ConnectHandler(socket =>
            {
                string socketId = socket.SocketId;
                socket.CloseHandler(() =>
                {
                    _output.WriteLine($"[{socketId}] client socket close handler");
                    closed = true;
                    countdownEvent.Signal();
                });
                socket.ExceptionHandler(exception =>
                {
                    _output.WriteLine($"[{socketId}] client socket close handler");
                });

                byte[] sendData = Encoding.UTF8.GetBytes(data);
                socket.Send(sendData, sendSize =>
                {
                    _output.WriteLine($"[{socketId}] client socket send  - {data}:{data.Length}");
                });
                socket.Receive(receivedData =>
                {
                    string stringData = Encoding.UTF8.GetString(receivedData);
                    _output.WriteLine($"[{socketId}] client socket receive  - {stringData}:{stringData.Length}");
                });
            });
            client.ExceptionHandler(exception =>
            {
                _output.WriteLine($"client connect exception handler - {exception}");
            });
            client.Connect("127.0.0.1", port);
            countdownEvent.Wait();
            server.Stop();
            Assert.True(closed);
        }
Example #41
0
        static int Main(string[] args)
        {
            var showHelp = false;
            var configPath = string.Empty;
            var crusherSectionName = "Crusher";
			var cssSpriteSectionName = "CssSprite";
            var applicationPath = "/";

            var options = new OptionSet()
            {
                {
                    "c=|configPath=",
                    "the configuration path to the web.config or app.config file. E.g. ../../../Talifun.Web.Examples/Crusher.Demo/web.config",
                    c => configPath = c
                },
                {
                    "cs=|crusherSectionName=",
                    "the section name of the configuration element for the Talifun.Crusher configuration. Defaults to 'Crusher' if not specified.",
                    cs => crusherSectionName = cs 
                },
                {
                    "css=|cssSpriteSectionName=",
                    "the section name of the configuration element for the Talifun.CssSprite configuration. Defaults to 'CssSprite' if not specified.",
                    css => cssSpriteSectionName = css 
                },
                {
                    "a=|applicationPath=",
                    "the application path to be relative from. Defaults to  '/' if not specified.",
                    a => applicationPath = a
                },
                {
                    "?|h|help", 
                    "display help screen",
                    h => showHelp = h != null
                }
            };
                
            try
            {
                options.Parse(args);
            }
            catch(OptionException e)
            {
                Console.WriteLine(HeaderMessage);
                Console.WriteLine(e.Message);
                Console.WriteLine(UsageMessage);
                Console.WriteLine(HelpMessage);
				return DisplayHelpScreenExitCode;
            }

            if (showHelp)
            {
                DisplayHelp(options);
				return DisplayHelpScreenExitCode;
            }

            if (string.IsNullOrEmpty(configPath))
            {
                Console.WriteLine(HeaderMessage);
                Console.WriteLine(UsageMessage);
                Console.WriteLine(HelpMessage);
				return DisplayHelpScreenExitCode;
            }

            var crusherConfiguration = GetCrusherSection(configPath, crusherSectionName);

			if (crusherConfiguration == null)
            {
                Console.WriteLine(HeaderMessage);
				Console.WriteLine("\"{0}\" section name not found in {1} ", crusherSectionName, configPath);
                Console.WriteLine(HelpMessage);
				return DisplayHelpScreenExitCode;
            }

        	try
        	{
				Console.WriteLine();
				Console.WriteLine("Settings used:");
				Console.WriteLine("configPath = " + configPath);
				Console.WriteLine("crusherSectionName = " + crusherSectionName);
				Console.WriteLine("applicationPath = " + applicationPath);

        	    var configUri = new Uri(configPath, UriKind.RelativeOrAbsolute);
                if (!configUri.IsAbsoluteUri)
                {
                    configUri = new Uri(Path.Combine(Environment.CurrentDirectory, configUri.ToString()));
                }

                var physicalApplicationPath = new FileInfo(configUri.LocalPath).DirectoryName;

				var retryableFileOpener = new RetryableFileOpener();
				var hasher = new Md5Hasher(retryableFileOpener);
				var retryableFileWriter = new RetryableFileWriter(BufferSize, Encoding, retryableFileOpener, hasher);
				var pathProvider = new PathProvider(applicationPath, physicalApplicationPath);
				var cacheManager = new HttpCacheManager();

                var fileMetaData = new MultiFileMetaData(retryableFileOpener, retryableFileWriter);

        	    var jsOutput = string.Empty;
        	    var cssOutput = string.Empty;
                var cssSpriteOutput = string.Empty;
     
                //We want to be able to use output from css sprites in crushed content
                var countdownEvents = new CountdownEvent(3);

                var cssSpriteExceptions = new List<CssSpriteException>();
                ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        if (crusherConfiguration != null)
                        {
                            var cssSpriteGroups = crusherConfiguration.CssSpriteGroups;
                            var cssSpriteCreator = new CssSpriteCreator(cacheManager, retryableFileOpener, pathProvider, retryableFileWriter, fileMetaData);
                            var cssSpriteGroupsProcessor = new CssSpriteGroupsProcessor();

                            cssSpriteOutput = cssSpriteGroupsProcessor.ProcessGroups(pathProvider, cssSpriteCreator, cssSpriteGroups).ToString();
                        }
                    }
                    catch (Exception exception)
                    {
                        cssSpriteExceptions.Add(new CssSpriteException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

                var jsExceptions = new List<JsException>();
                ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        if (crusherConfiguration != null)
                        {
                            var jsCrusher = new JsCrusher(cacheManager, pathProvider, retryableFileOpener, retryableFileWriter, fileMetaData);
                            var jsGroups = crusherConfiguration.JsGroups;
                            var jsGroupsProcessor = new JsGroupsProcessor();

                            jsOutput = jsGroupsProcessor.ProcessGroups(pathProvider, jsCrusher, jsGroups).ToString();
                        }
                    }
                    catch (Exception exception)
                    {
                        jsExceptions.Add(new JsException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

                var cssExceptions = new List<CssException>();
                ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        if (crusherConfiguration != null)
                        {
                            var hashQueryStringKeyName = crusherConfiguration.QuerystringKeyName;
                            var cssAssetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, hasher, pathProvider);
                            var cssPathRewriter = new CssPathRewriter(cssAssetsFileHasher, pathProvider);
                            var cssCrusher = new CssCrusher(cacheManager, pathProvider, retryableFileOpener, retryableFileWriter, cssPathRewriter, fileMetaData, crusherConfiguration.WatchAssets);
                            var cssGroups = crusherConfiguration.CssGroups;
                            var cssGroupsCrusher = new CssGroupsProcessor();
                            cssOutput = cssGroupsCrusher.ProcessGroups(pathProvider, cssCrusher, cssGroups).ToString();
                        }
                    }
                    catch (Exception exception)
                    {
                        cssExceptions.Add(new CssException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

        	    countdownEvents.Wait();

                if (string.IsNullOrEmpty(cssSpriteOutput) && !cssSpriteExceptions.Any())
                {
                    Console.WriteLine();
                    Console.WriteLine("Skipping css sprite creation. \"{0}\" section name not found in \"{1}\"", cssSpriteSectionName, configPath);
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine(cssSpriteOutput);

                    if (cssSpriteExceptions.Any())
                    {
                        Console.WriteLine("Css sprite errors:");
                        Console.WriteLine(new AggregateException(cssSpriteExceptions.Cast<Exception>()));
                    }
                }

                if (string.IsNullOrEmpty(jsOutput) && string.IsNullOrEmpty(cssOutput) && !jsExceptions.Any() && !cssExceptions.Any())
                {
                    Console.WriteLine();
                    Console.WriteLine("Skipping css/js crushed content creation. \"{0}\" section name not found in \"{1}\"", crusherSectionName, configPath);
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine(cssOutput);
                    if (cssExceptions.Any())
                    {
                        Console.WriteLine("Css errors:");
                        Console.WriteLine(new AggregateException(cssExceptions.Cast<Exception>()));
                    }

                    Console.WriteLine();
                    Console.WriteLine(jsOutput);
                    if (jsExceptions.Any())
                    {
                        Console.WriteLine("Js errors:");
                        Console.WriteLine(new AggregateException(jsExceptions.Cast<Exception>()));
                    }
                }
			}
			catch (Exception exception)
			{
				Console.Write(exception);
				return ErrorExitCode;
			}

			return SuccessExitCode;
        }
Example #42
0
        protected override void Execute(CodeActivityContext context)
        {
            string attribute_str   = Attribute.Get(context);
            string attribute_value = Value.Get(context);

            try
            {
                Int32 _timeout = TimeoutMS.Get(context);
                Thread.Sleep(_timeout);
                latch = new CountdownEvent(1);
                Thread td = new Thread(() =>
                {
                    if (Selector.Expression == null)
                    {
                        //ActiveElement处理
                    }
                    else
                    {
                        var selStr        = Selector.Get(context);
                        UiElement element = GetValueOrDefault(context, this.Element, null);
                        if (element == null && selStr != null)
                        {
                            element = UiElement.FromSelector(selStr);
                        }
                        if (element != null)
                        {
                            element.SetForeground();
                            mshtml.IHTMLDocument2 currDoc      = null;
                            SHDocVw.InternetExplorer ieBrowser = GetIEFromHWndClass.GetIEFromHWnd((int)element.WindowHandle, out currDoc);
                            mshtml.IHTMLElement currEle        = GetIEFromHWndClass.GetEleFromDoc(
                                element.GetClickablePoint(), (int)element.WindowHandle, currDoc);
                            currEle.setAttribute(attribute_str, attribute_value);
                        }
                        else
                        {
                            SharedObject.Instance.Output(SharedObject.enOutputType.Error, "有一个错误产生", "查找不到元素");
                            if (ContinueOnError.Get(context))
                            {
                                return;
                            }
                            else
                            {
                                throw new NotImplementedException("查找不到元素");
                            }
                        }
                    }
                    refreshData(latch);
                });
                td.TrySetApartmentState(ApartmentState.STA);
                td.IsBackground = true;
                td.Start();
                latch.Wait();
            }
            catch (Exception e)
            {
                SharedObject.Instance.Output(SharedObject.enOutputType.Error, "出现异常", e.Message);
                if (ContinueOnError.Get(context))
                {
                }
            }
        }
Example #43
0
            public override void Run()
            {
                using (Stream stream = new NetworkStream(cs))
                {
                    BinaryReader intReader = new BinaryReader(stream);
                    BinaryWriter intWriter = new BinaryWriter(stream);
                    try
                    {
                        int id = intReader.ReadInt32();
                        if (id < 0)
                        {
                            throw new IOException("Client closed connection before communication started.");
                        }

                        startingGun.Wait();
                        intWriter.Write(43);
                        stream.Flush();

                        while (true)
                        {
                            int command = stream.ReadByte();
                            if (command < 0)
                            {
                                return; // closed
                            }

                            lock (localLock)
                            {
                                int currentLock = lockedID[0];
                                if (currentLock == -2)
                                {
                                    return; // another thread got error, so we exit, too!
                                }
                                switch (command)
                                {
                                case 1:
                                    // Locked
                                    if (currentLock != -1)
                                    {
                                        lockedID[0] = -2;
                                        throw new InvalidOperationException("id " + id + " got lock, but " + currentLock + " already holds the lock");
                                    }
                                    lockedID[0] = id;
                                    break;

                                case 0:
                                    // Unlocked
                                    if (currentLock != id)
                                    {
                                        lockedID[0] = -2;
                                        throw new InvalidOperationException("id " + id + " released the lock, but " + currentLock + " is the one holding the lock");
                                    }
                                    lockedID[0] = -1;
                                    break;

                                default:
                                    throw new Exception("Unrecognized command: " + command);
                                }
                                intWriter.Write((byte)command);
                                stream.Flush();
                            }
                        }
                    }
                    catch (IOException ioe)
                    {
                        throw new Exception(ioe.ToString(), ioe);
                    }
                    catch (Exception e)
                    {
                        // LUCENENET NOTE: We need to throw a new exception
                        // to ensure this is Exception and not some other type.
                        throw new Exception(e.ToString(), e);
                    }
                    finally
                    {
                        IOUtils.DisposeWhileHandlingException(cs);
                    }
                }
            }
Example #44
0
        static void Impl(int disposeAt)
        {
            var rand = new Random();

            var g = new CompositeDisposable();

            Console.Write("Dispose @ = {0} - ", disposeAt);

            if (disposeAt == 0)
            {
                g.Dispose();
                Console.Write("{GD} ");
            }

            if (disposeAt == 1)
            {
                var sleep = rand.Next(0, 5) > 1 /* 60% chance */ ? rand.Next(2, 1000) : 0;

                ThreadPool.QueueUserWorkItem(_ =>
                {
                    Helpers.SleepOrSpin(sleep);
                    g.Dispose();
                    Console.Write("{GD} ");
                });
            }

            var n  = rand.Next(0, 1000);
            var cd = new CountdownEvent(n);

            var ds = Enumerable.Range(0, n).Select(_ => Disposable.Create(() => cd.Signal())).ToArray();

            var m    = rand.Next(1, 100);
            var jobs = ds.GroupBy(_ => rand.Next() % m).Select(Enumerable.ToList).ToList();

            Console.Write("N = {0}, M = {1} - ", n, m);

            var done = new CountdownEvent(jobs.Count);

            foreach (var job in jobs)
            {
                var sleep   = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;
                var sleepAt = Enumerable.Range(0, rand.Next(0, job.Count) / rand.Next(1, 100)).ToArray();
                var sleeps  = sleepAt.Select(_ => rand.Next(0, 50)).ToArray();

                var rem   = rand.Next(0, 3) == 0; /* 33% chance */
                var remAt = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0;

                var mine = job;
                ThreadPool.QueueUserWorkItem(_ =>
                {
                    Helpers.SleepOrSpin(sleep);

                    var j = 0;
                    foreach (var d in mine)
                    {
                        var dd = d;

                        if (sleepAt.Contains(j))
                        {
                            Helpers.SleepOrSpin(sleeps[j]);
                        }

                        g.Add(dd);
                        Console.Write("+");

                        if (rem)
                        {
                            ThreadPool.QueueUserWorkItem(__ =>
                            {
                                Helpers.SleepOrSpin(remAt);
                                g.Remove(dd);
                                Console.Write("-");
                            });
                        }

                        j++;
                    }

                    done.Signal();
                });
            }

            done.Wait();

            if (disposeAt == 2)
            {
                g.Dispose();
                Console.Write("{GD} ");
            }

            cd.Wait();

            Console.WriteLine(".");
        }
Example #45
0
        public void TestEtw()
        {
            using (var listener = new TestEventListener(new Guid("16F53577-E41D-43D4-B47E-C17025BF4025"), EventLevel.Verbose))
            {
                ActionBlock<int> ab = null;
                BufferBlock<int> bb = null;
                int remaining = 0;
                CountdownEvent ce = new CountdownEvent(0);

                // Check that block creation events fire
                const int DataflowBlockCreatedId = 1;
                remaining = 2;
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: DataflowBlockCreatedId, actual: ev.EventId);
                        remaining--;
                    },
                    () => {
                        ab = new ActionBlock<int>(i => { });
                        bb = new BufferBlock<int>(); // trigger block creation event
                        Assert.Equal(expected: 0, actual: remaining);
                    });

                // Check that linking events fire
                const int BlockLinkedId = 4;
                remaining = 1;
                IDisposable link = null;
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: BlockLinkedId, actual: ev.EventId);
                        remaining--;
                    },
                    () => {
                        link = bb.LinkTo(ab);
                        Assert.Equal(expected: 0, actual: remaining);
                    });

                // Check that unlinking events fire
                const int BlockUnlinkedId = 5;
                remaining = 1;
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: BlockUnlinkedId, actual: ev.EventId);
                        remaining--;
                    },
                    () => {
                        link.Dispose();
                        Assert.Equal(expected: 0, actual: remaining);
                    });

                // Check that task launched events fire
                const int TaskLaunchedId = 2;
                ce.Reset(1);
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: TaskLaunchedId, actual: ev.EventId);
                        ce.Signal();
                    },
                    () => {
                        ab.Post(42);
                        ce.Wait();
                        Assert.Equal(expected: 0, actual: ce.CurrentCount);
                    });

                // Check that completion events fire
                const int BlockCompletedId = 3;
                ce.Reset(2);
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: BlockCompletedId, actual: ev.EventId);
                        ce.Signal();
                    },
                    () => {
                        ab.Complete();
                        bb.Complete();
                        ce.Wait();
                        Assert.Equal(expected: 0, actual: ce.CurrentCount);
                    });

            }
        }
        public object Clone()
        {
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
            try
            {
                var cssSpriteOutput = string.Empty;
                var jsOutput = string.Empty;
                var cssOutput = string.Empty;

                var countdownEvents = new CountdownEvent(1);

                ThreadPool.QueueUserWorkItem(data =>
                    {
                        var countdownEvent = (CountdownEvent) data;

                        try
                        {
                            if (_cssSpriteConfiguration != null)
                            {
                                var cssSpriteGroups = _cssSpriteConfiguration.CssSpriteGroups;
                                var cssSpriteCreator = new CssSpriteCreator(_cacheManager, _retryableFileOpener, _pathProvider, _retryableFileWriter, _fileMetaData);
                                var cssSpriteGroupsProcessor = new CssSpriteGroupsProcessor();

                                cssSpriteOutput = cssSpriteGroupsProcessor.ProcessGroups(_pathProvider, cssSpriteCreator, cssSpriteGroups).ToString();

                                _logMessage(cssSpriteOutput);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logError(exception.ToString());
                        }
                        countdownEvent.Signal();
                    }, countdownEvents);

                countdownEvents.Wait();

                countdownEvents = new CountdownEvent(2);

                ThreadPool.QueueUserWorkItem(data =>
                    {
                        var countdownEvent = (CountdownEvent) data;

                        try
                        {
                            if (_crusherConfiguration != null)
                            {
                                var jsCrusher = new JsCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, _fileMetaData);
                                var jsGroups = _crusherConfiguration.JsGroups;
                                var jsGroupsProcessor = new JsGroupsProcessor();

                                jsOutput = jsGroupsProcessor.ProcessGroups(_pathProvider, jsCrusher, jsGroups).ToString();

                                _logMessage(jsOutput);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logError(exception.ToString());
                        }
                        countdownEvent.Signal();
                    }, countdownEvents);

                ThreadPool.QueueUserWorkItem(data =>
                    {
                        var countdownEvent = (CountdownEvent) data;

                        try
                        {
                            if (_crusherConfiguration != null)
                            {
                                var hashQueryStringKeyName = _crusherConfiguration.QuerystringKeyName;
                                var cssAssetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, _hasher, _pathProvider);
                                var cssPathRewriter = new CssPathRewriter(cssAssetsFileHasher, _pathProvider);
                                var cssCrusher = new CssCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, cssPathRewriter, _fileMetaData, _crusherConfiguration.WatchAssets);
                                var cssGroups = _crusherConfiguration.CssGroups;
                                var cssGroupsCrusher = new CssGroupsProcessor();
                                cssOutput = cssGroupsCrusher.ProcessGroups(_pathProvider, cssCrusher, cssGroups).ToString();

                                _logMessage(cssOutput);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logError(exception.ToString());
                        }
                        countdownEvent.Signal();
                    }, countdownEvents);

                countdownEvents.Wait();
            }
            catch (Exception exception)
            {
                _logError(exception.ToString());
            }
            finally
            {
                AppDomain.CurrentDomain.UnhandledException -= OnUnhandledException;
            }

            return null;
        }