Skip to content

Equivalent of BeginInvoke for delaying work #1056

Answered by AArnott
jaredpar asked this question in Q&A
Discussion options

You must be logged in to vote

This is the closest equivalent:

_ = JoinableTaskFactory.RunAsync( 
  async () => {
    await Task.Yield();
    // work to be done later 
  });

This requires the condition you met that you know you're already on the UI thread. It will use the default priority for that JTF instance to get back to the UI thread once you get off of it.
If you didn't know you were on the main thread already, using SwitchToMainThreadAsync(alwaysYield: true) as @Therzok said is the best way to go since you want the delegate to run on the main thread.
WithPriority allocates a new object, so if you were to use that in high traffic code, you might want to use it once and store the result in a field for reuse.

If th…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
2 replies
@sharwell
Comment options

@jaredpar
Comment options

Comment options

You must be logged in to vote
3 replies
@jaredpar
Comment options

@AArnott
Comment options

@jaredpar
Comment options

Answer selected by jaredpar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants