Пример #1
0
        public void ReferencePassingTest()
        {
            var data = new DataBag();

            ThreadingHelpers.ExecuteOnThreadPool(
                () => data.X++,
                10);

            Assert.Equal(10, data.X);
        }
Пример #2
0
        static void Main(string[] args)
        {
            // Here you can play around with those method, prototype and easily debug

            Console.WriteLine($"Main thread is {Thread.CurrentThread.ManagedThreadId}");

            // Generate a token that's immediately cancelled
            CancellationTokenSource cts = new CancellationTokenSource();

            cts.Cancel();
            CancellationToken cancelToken = cts.Token;

            // new CancellationTokenSource(200).Token

            /*
             * ThreadingHelpers.ExecuteOnThread(() =>
             * {
             *  var thread = Thread.CurrentThread;
             *  Console.WriteLine($"Hello from thread {thread.ManagedThreadId} from a pool: {thread.IsThreadPoolThread}");
             *  Thread.Sleep(1000);
             *  //throw new StackOverflowException();
             * }, 3, cancelToken,
             * (System.Exception exception) => { Console.WriteLine($"<{exception.Message}> occurred !"); });
             */

            /*
             * ThreadingHelpers.ExecuteOnThread(
             *  () => throw new NullReferenceException(),
             *  10,
             *  new CancellationTokenSource(1000).Token,
             *  errorAction: (System.Exception exception) => { Console.WriteLine($"<{exception.Message}> occurred !"); });
             */

            /*
             * ThreadingHelpers.ExecuteOnThreadPool(() =>
             * {
             *  var thread = Thread.CurrentThread;
             *  Console.WriteLine(
             *      $"Hello from thread {thread.ManagedThreadId} from a pool: {thread.IsThreadPoolThread}");
             * }, 3);
             */


            ThreadingHelpers.ExecuteOnThreadPool(
                () => Thread.Sleep(100),
                3,
                cancelToken,
                errorAction: (System.Exception exception) => { Console.WriteLine($"<{exception.Message}> occurred !"); }
                );
        }
Пример #3
0
        public void TimeoutCancellationTest()
        {
            var errorActionMock = new Mock <Action <Exception> >();

            CancellationTokenSource cts = new CancellationTokenSource();

            cts.CancelAfter(200);

            ThreadingHelpers.ExecuteOnThreadPool(
                () => Thread.Sleep(100),
                10,
                cts.Token,
                errorAction: errorActionMock.Object);

            errorActionMock.Verify(m => m(It.IsAny <OperationCanceledException>()), Times.Once);
        }
Пример #4
0
        static void Main(string[] args)
        {
            // Here you can play around with those method, prototype and easily debug

            Console.WriteLine($"Main thread is {Thread.CurrentThread.ManagedThreadId}");

            ThreadingHelpers.ExecuteOnThread(() =>
            {
                var thread = Thread.CurrentThread;
                Console.WriteLine($"Hello from thread {thread.ManagedThreadId} from a pool: {thread.IsThreadPoolThread}");
            }, 3);

            ThreadingHelpers.ExecuteOnThreadPool(() =>
            {
                var thread = Thread.CurrentThread;
                Console.WriteLine(
                    $"Hello from thread {thread.ManagedThreadId} from a pool: {thread.IsThreadPoolThread}");
            }, 3);
        }
Пример #5
0
        public void RunningOnDifferentThreadTest()
        {
            var  mainThreadId         = Thread.CurrentThread.ManagedThreadId;
            var  testThreadId         = 0;
            bool?testThreadIsFromPool = null;

            ThreadingHelpers.ExecuteOnThreadPool(
                () =>
            {
                Thread.Sleep(100);
                testThreadId         = Thread.CurrentThread.ManagedThreadId;
                testThreadIsFromPool = Thread.CurrentThread.IsThreadPoolThread;
            }, 1);

            Assert.NotEqual(0, testThreadId);
            Assert.NotEqual(mainThreadId, testThreadId);
            Assert.NotNull(testThreadIsFromPool);
            Assert.True(testThreadIsFromPool);
        }
 public void ExecuteOnThreadPool()
 {
     ThreadingHelpers.ExecuteOnThreadPool(() => sha256.ComputeHash(data), 1);
 }