---
title: "ThreadTester with new Ability: StopWhenTrue() and thread polling"
url: "https://osherove.com/blog/2007/6/25/threadtester-with-new-ability-stopwhentrue-and-thread-pollin.md"
canonical: "https://osherove.com/blog/2007/6/25/threadtester-with-new-ability-stopwhentrue-and-thread-pollin.html"
published: "2007-06-25T11:34:34"
updated: "2007-06-25T11:34:34"
tags: ["Agile", "Tips & Tricks", "Unit Testing"]
author: "Roy Osherove"
description: "I've updated The ThreadTester Library with a new ability: StopWhenTrue(). It allows you to periodically poll while all the threads are running (and the test is blocking) and either assert stuff or stop the run midway…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# ThreadTester with new Ability: StopWhenTrue() and thread polling

By Roy Osherove June 25, 2007 In [Agile](https://osherove.com/blog/category/agile), [Tips & Tricks](https://osherove.com/blog/category/tips-tricks), [Unit Testing](https://osherove.com/blog/category/unit-testing)

**Historical article** · Originally published June 25, 2007; technical details may be outdated.

I've updated The [ThreadTester](http://weblogs.asp.net/rosherove/archive/2007/06/22/multi-threaded-unit-tests-with-osherove-threadtester.aspx) Library with a new ability: StopWhenTrue().

It allows you to periodically poll while all the threads are running (and the test is blocking) and either assert stuff or stop the run midway when some condition is true. 

- Download [source](http://files.osherove.com/downloads/ThreadTester.src.zip) and [binary](http://files.osherove.com/downloads/Osherove.ThreadTester.zip).
- Here's the [source in SVN](http://tools.assembla.com/svn/NTestEx).

Here's a test that shows how this is possible with the new API:

```
[Test]
public void StopWhenTrue_ReturnTrueWhenCountOver1000_AllThreadsStopImmediately()
{
 Counter c = new Counter();
 ThreadTester tt = new ThreadTester();
 tt.RunBehavior=ThreadRunBehavior.RunForSpecificTime;
 tt.AddThreadAction(delegate
             {
                for (int j = 0; j < 103; j++)
                  {
                                               c.Increment();
                                           }
                                           Thread.Sleep(50);
                                       });
 
 tt.StopWhenTrue(delegate
                  {
                    // you can also assert stuff here
                    Console.WriteLine("currently at " + c.Count);
                     return c.Count > 1000;
                  },100);// check every 100ms
 
          //this test will end in less than a second because of our added functionality
tt.StartAllThreads(10000); 
 Assert.Greater(c.Count,1000);
 Assert.Less(c.Count,1050);
}
```

## More in Agile

[All posts in Agile](https://osherove.com/blog/category/agile)

- Jun 25, 2007

    ### [Implementing Unit Test MetaData using XtUnit custom attributes](https://osherove.com/blog/2007/6/25/implementing-unit-test-metadata-using-xtunit-custom-attribut.html)

- Jun 25, 2007

    ### [Unit Test Tip: Encode test data as part of your test xml comments](https://osherove.com/blog/2007/6/25/unit-test-tip-encode-test-data-as-part-of-your-test-xml-comm.html)

- Jun 24, 2007

    ### [Agile Israel Conference 2007 - Call For Speakers](https://osherove.com/blog/2007/6/24/agile-israel-conference-2007-call-for-speakers.html)

- Jun 22, 2007

    ### [Multi Threaded Unit Tests with Osherove.ThreadTester](https://osherove.com/blog/2007/6/22/multi-threaded-unit-tests-with-osherovethreadtester.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
