title: Progress bar with async in FsXaml application
body: <p>In my <strong>F#</strong> <strong>(FsXaml/Code Behind)</strong> application, I would like to use a progress bar without the utilisation of a background worker as I do in C#. Based on an article on the internet (the link is <a href="https://teadrivendev.github.io/2015/12/11/making-progress-fsharp/" rel="nofollow noreferrer">here</a>), I tried to use asynchronous workflows.</p>
  <p>I created code based (to some extend) on the examples in the aforementioned article, but it did not work as I had expected. The current thread (UI thread) is still blocked as if no async code was there. No switching to a background thread occurs. The progress bar is activated only after the long running operation has been finished. Removing the <em>onThreadPool</em> function has no effect.</p>
  <p>My question is: What is wrong in my code and how to make it right?</p>
  <pre><code>type MainWindowXaml = FsXaml.XAML&lt;&quot;XAMLAndCodeBehind/MainWindow.xaml&quot;&gt;
  
  type MainWindow() as this =
  
      inherit MainWindowXaml()
  
      //....some code....
  
      let ButtonClick _ = 
     
          //....some code....
         
          let longRunningOperation() = //....some long running operation (reading from Google Sheets)....            
               
          let progressBar() = this.ProgressBar.IsIndeterminate &lt;- true     
  
          let doBusyAsync progress operation =  
              progress
              async
                  {   
                    do! operation
                  }
              |&gt; Async.StartImmediate 
      
          let onThreadPool operation =
              async
                  {    
                    let context = System.Threading.SynchronizationContext.Current
                    do! Async.SwitchToThreadPool()
                    let! result = operation
                    do! Async.SwitchToContext context
                    return result
                  } 
      
          let asyncOperation progress operation =   
              async { operation } 
              |&gt; onThreadPool
              |&gt; doBusyAsync progress 
      
          (progressBar(), longRunningOperation()) ||&gt; asyncOperation 
        
      do
          //....some code....
          this.Button.Click.Add ButtonClick
  </code></pre>