Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7628943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:42:32+00:00 2026-05-31T05:42:32+00:00

This question is based on Async.TryCancelled doesn't work with Async.RunSynchronously that looks complex, so

  • 0

This question is based on Async.TryCancelled doesn't work with Async.RunSynchronously that looks complex, so I will cut a simple part that I try to solve.

Suppose I have this functions:

let prepareModel () = 
    async {
        // this might take a lot of time (1-50seconds)
        let! a = ...
        let! b = ...
        let! res = combine a b
        return res
    }
 let updateUI model =
    runOnUIThread model

prepareModel prepares data that should be displayed to the user. updateUI refreshes the UI (removes old controls and creates new ctls based on new data).

Question: How should I call the two functions so that prepareModel is cancellable any time?

The flow is

  • user clicks refresh
    • prepareModel(1) started and is running asynchronously, so the UI is responsive and user can work with the application
  • user changes data and clicks refresh again
    • prepareModel(1) from is cancelled and
      new prepareModel(2) is started
  • user changes data and clicks refresh again
    • prepareModel(2) is cancelled and
      new prepareModel(3) is started
    • ..
    • prepareModel(n) finished
    • updateUI is ran on UI thread, redraws the UI

(My first solution is based on MailboxProcessor that ensures that only one prepareModel is executed, see at Async.TryCancelled doesn't work with Async.RunSynchronously but as I experimented with this, it’s not bug free)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:42:33+00:00Added an answer on May 31, 2026 at 5:42 am

    One possible approach would be to start the workflow asynchronously in the background using Async.Start (then it should be cancellable). To redraw the UI at the end, you can use Async.SwitchToContext to make sure that the last part of the workflow executes on the UI. Here is a sketch:

    // Capture current synchronization context of the UI
    // (this should run on the UI thread, i.e. when starting)
    let syncContext = System.Threading.SynchronizationContext.Current
    
    // Cancellation token source that is used for cancelling the
    // currently running workflow (this can be mutable)
    let cts = ref (new CancellationTokenSource())
    
    // Workflow that does some calculations and then updates gui
    let updateModel () =   
        async {  
            // this might take a lot of time (1-50seconds)  
            let! a = ...  
            let! b = ...  
            let! res = combine a b 
    
            // switch to the GUI thread and update UI
            do! Async.SwitchToContext(syncContext)
            updateUserInterface res
        }  
    
    // This would be called in the click handler - cancel the previous
    // computation, creat new cancellation token & start the new one
    cts.Cancel()
    cts := new CancellationTokenSource()
    Async.Start(updateModel(), cts.Token)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on this question it appears that the default template for CheckStyle will allow
This question is based on a previous , but that's just FYI. I've managed
Based on this question Regex \d+(?:-\d+)+ will match this 10-3-1 and 5-0 . Example:
UPDATE (2010-12-21): Completely rewrote this question based on tests that I've been doing. Also,
for me I will answer this question based on my experience. so if I'm
Another networking question this time related to an Async Socket Client which is based
This question is based on another question of mine (thankfully answered). So if in
This question is based on the thread . I have the shell function function
This question is based on this thread . Is [---] a comment in Git
This question is based on this thread . I am interested in how Git

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.