Share via


CCR tips and tricks - part 10

Today we'll continue using the same base helpers as in part 9 and today we'll look at a common mistake made in iterative handlers. Take a look at the following code:

   1: private IEnumerator<ITask> ForgettingToYield(ManualResetEvent mre)
  2: {
  3:     var port = new Port<EmptyValue>();
  4:     port.Post(EmptyValue.SharedInstance);
  5:     Arbiter.Receive(false, port, v => mre.Set());
  6:     yield break;
  7: }
  8:         
  9: [TestMethod]
 10: public void Forgetting_to_yield()
 11: {
 12:     var mre = new ManualResetEvent(false);
 13:     Arbiter.Activate(
 14:         dispatcherQueue, 
 15:         Arbiter.FromIteratorHandler(() => this.ForgettingToYield(mre)));
 16:     Assert.IsFalse(
 17:         mre.WaitOne(TimeSpan.FromSeconds(3)), "Should not complete tasks");
 18: }

Note the mistake on line 5. Since the task created is not yielded on it will not execute. In my experience these type of errors are easily found while writing unit tests. They look weird sine you think "why is this not executing" even though the code "look" right.