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 802461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:36:23+00:00 2026-05-14T23:36:23+00:00

I had a situation come up that required running a lambda expression on the

  • 0

I had a situation come up that required running a lambda expression on the UI thread after a delay. I thought of several ways to do this and finally settled on this approach

Task.Factory.StartNew(() => Thread.Sleep(1000))
    .ContinueWith((t) => textBlock.Text="Done",TaskScheduler.FromCurrentSynchronizationContext());

But I’m wondering if there’s an easier way that I missed. Any suggestions for a shorter, simpler or easier technique? Assume .NET 4 is available.

  • 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-14T23:36:24+00:00Added an answer on May 14, 2026 at 11:36 pm

    I think what you’ve got is pretty good Scott.

    The only slight issue I think some might have with it, is that you’re blocking a thread in order to execute your delay. Of course it’s a background thread, and unlikely to cause problems unless you execute a lot of these calls concurrently (each tying up a thread), but it’s still probably suboptimal.

    I would instead suggest that you factor the algorithm into a utility method, and avoid using Thread.Sleep.

    There’s obviously probably innumerable ways of doing this, but here’s one:

    public static class UICallbackTimer
    {
        public static void DelayExecution(TimeSpan delay, Action action)
        {
            System.Threading.Timer timer = null;
            SynchronizationContext context = SynchronizationContext.Current;
    
            timer = new System.Threading.Timer(
                (ignore) =>
                {
                    timer.Dispose();
    
                    context.Post(ignore2 => action(), null);
                }, null, delay, TimeSpan.FromMilliseconds(-1));
        }
    }
    

    To use:

        UICallbackTimer.DelayExecution(TimeSpan.FromSeconds(1),
            () => textBlock.Text="Done");
    

    Of course you could also write an implementation of this DelayExecution method which uses other types of timer such as the WPF DispatcherTimer or the WinForms Timer class. I’m not sure what the tradeoffs of these various timers would be. My guess would be DispatcherTimer’s and WinForm’s timers would actually still function on applications of the opposite type.

    EDIT:

    Re-reading my answer, I think actually I would be tempted to factor this into an extension method which works on synchronization contexts – if you think about it, a more general statement would be that you need to be able to post work back to a synchronization context after a certain delay.

    The SynchronizationContext already has a post method for queueing work, which the original caller does not want to block on completion. What we need is a version of this that posts the work after a delay, so instead:

    public static class SyncContextExtensions
    {
        public static void Post(this SynchronizationContext context, TimeSpan delay, Action action)
        {
            System.Threading.Timer timer = null;
    
            timer = new System.Threading.Timer(
                (ignore) =>
                {
                    timer.Dispose();
    
                    context.Post(ignore2 => action(), null);
                }, null, delay, TimeSpan.FromMilliseconds(-1));
        }
    }
    

    and use:

            SynchronizationContext.Current.Post(TimeSpan.FromSeconds(1),
                () => textBlock.Text="Done");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 407k
  • Answers 407k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Unfortunately they have not been implemented (much to the annoyance… May 15, 2026 at 6:37 am
  • Editorial Team
    Editorial Team added an answer Prior to closing the form, you could notify the thread… May 15, 2026 at 6:37 am
  • Editorial Team
    Editorial Team added an answer Use the :eq selector: $("#Grid td:first-child").click(function() { var value =… May 15, 2026 at 6:37 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.