title: Stop method and loop in callback
body: <p><strong>Question 1.</strong> I wonder if it's possible to stop the loop in the inside operation's callback?<br />
  <strong>Question 2.</strong> Can I stop <code>SomeMethod()</code> in the callback?</p>
  
  <p>E.g. I have a code like:</p>
  
  <pre><code>foreach(...)
  {
     myObject.SomeMethod(s =&gt;
        {
           // something break-like here to stop foreach/method?
        });
  }
  </code></pre>
  
  <p><strong>[edit]</strong></p>
  
  <p>Here's the example of the code I use - it doesn't work as I want.</p>
  
  <pre><code>bool test = false;
  foreach (var drive in drives)
  {
      foundFolders.AddRange(
          DirectoryWrapper.GetDirectories(drive, regex, true, s =&gt;
              {
                  Dispatcher.Invoke(new Action(() =&gt; SetWarningStatus(WarningTypes.Warning,
                                                                      "Looking for backups: " + Environment.NewLine + s.Trim())), DispatcherPriority.Background);
                  test = true;
                  return;
              }));
      if (test)
          break;
  }
  </code></pre>
  
  <p>Even <code>Resharper</code> says that <code>return</code> is redundant here...</p>
  
  <p><strong>Solution</strong><br />
  After @Tigran's suggestion I noticed that always what I should do is to change my <code>GetDirectories</code> definition.</p>
  
  <p>From:</p>
  
  <pre><code>public static IEnumerable&lt;string&gt; GetDirectories(string root, string searchRegex, bool skipSystemDirs = false, Action&lt;string&gt; callback = null) {}
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>public delegate bool MyCallback(string s);
  public static IEnumerable&lt;string&gt; GetDirectories(string root, string searchRegex, bool skipSystemDirs = false, MyCallback callback = null)
  </code></pre>
  
  <p>Then I can return a flag inside a callback function and serve it inside <code>GetDirectories()</code>.</p>
  
  <p>Btw. what's interesting, when we have our <code>"GetDirectories"</code> as a binary, then we probably can't stop it in delegate... We have to wait until its execution finishes.</p>