private void button2_Click_1(object sender, EventArgs e) { t.AbortAll(); button2.Enabled = false; while (t.HasRunningThreads()) { button2.Enabled = false; Application.DoEvents(); } Text = "Done"; button1.Enabled = button2.Enabled = true; }
public void AbortAll_WorkNeverCompleted() { CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); TaskThreadManager uut = new TaskThreadManager(10, cancellationTokenSource); int count = 0; uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.AbortAll(); System.Threading.Thread.Sleep(250); Assert.IsTrue(count < 5); }
public void AbortAll_WorkNeverCompleted() { var cancellationTokenSource = new CancellationTokenSource(); var uut = new TaskThreadManager(10, cancellationTokenSource); var count = 0; uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); }); //cancellationTokenSource.Cancel(); uut.AbortAll(); System.Threading.Thread.Sleep(600); Assert.IsTrue(count < 10, "Completed all 10 tasks but should not have"); }