public void ReferencePassingTest() { var data = new DataBag(); ThreadingHelpers.ExecuteOnThread( () => data.X++, 10); Assert.Equal(10, data.X); }
public void TimeoutCancellationTest() { var errorActionMock = new Mock <Action <Exception> >(); CancellationTokenSource cts = new CancellationTokenSource(); cts.CancelAfter(200); ThreadingHelpers.ExecuteOnThread( () => Thread.Sleep(100), 10, cts.Token, errorAction: errorActionMock.Object); errorActionMock.Verify(m => m(It.IsAny <OperationCanceledException>()), Times.Once); }
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); }
public void RunningOnDifferentThreadTest() { var mainThreadId = Thread.CurrentThread.ManagedThreadId; var testThreadId = 0; bool?testThreadIsFromPool = null; ThreadingHelpers.ExecuteOnThread( () => { Thread.Sleep(100); testThreadId = Thread.CurrentThread.ManagedThreadId; testThreadIsFromPool = Thread.CurrentThread.IsThreadPoolThread; }, 1); Assert.NotEqual(0, testThreadId); Assert.NotEqual(mainThreadId, testThreadId); Assert.NotNull(testThreadIsFromPool); Assert.False(testThreadIsFromPool); }
public void ExecuteOnThread() { ThreadingHelpers.ExecuteOnThread(() => sha256.ComputeHash(data), 1); }