title: Async.fromBeginEnd with WinForms Begin/EndInvoke
body: <p>I'm trying to write an Async wrapper on top of a function called listControls which extracts a list of Forms controls starting from a root control. The plan is to implement it in terms of Control.BeginInvoke, Control.EndInvoke and Async.FromBeginEnd</p>
  
  <p>The code is below:</p>
  
  <pre><code>type private ListControls = delegate of unit -&gt; Control list
  
  let asyncListControls (root:Forms.Control) : Async&lt;Control list&gt; = 
      let beginInvoke (_, _) = 
          new ListControls(fun () -&gt; listControls root)
          |&gt; root.BeginInvoke
  
      let endInvoke result = 
          root.EndInvoke result :?&gt; Control list
  
      Async.FromBeginEnd (beginInvoke, endInvoke)
  </code></pre>
  
  <p>The behavior that I'm getting is that the <strong>endInvoke</strong> is never executed. Anybody knows what am I doing wrong? Is there a better approach?</p>
  
  <p><em>EDIT</em></p>
  
  <p>Just to make it clear, this computation is executed eventually with Async.RunSynchronously.</p>
  
  <p>Also if I replace this code with </p>
  
  <pre><code>/// code below is reproduced from memory and might not compile
  let asyncListControls (root:Forms.Control) : Async&lt;Control list&gt; =
       let ctx = WindowsFormsSynchronizationContext.Current
       async {
           do! Async.SwitchToContext ctx
           return listControls root
           do! Async.SwitchToThreadPool ()
       }
  </code></pre>
  
  <p>...then it seems to work but I don't like it because of the forced context switches and the fact that the async computation is supposed to be built on the UI thread</p>