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

  • Home
  • SEARCH
  • 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 611529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:47:38+00:00 2026-05-13T17:47:38+00:00

I’ve recently tried to implement an asynchronous callback model, so that when developers consume

  • 0

I’ve recently tried to implement an asynchronous callback model, so that when developers consume my library, they don’t need to worry about the following:

if(control.InvokeRequried) { // invoke } 

That part of the code seems to be working quite nicely, but I discovered something a bit strange today, that I think I’ve diagnosed but would like to see what others with more experience think, and ways I might be able to resolve it.

The problem I’ve got, is it seems that my posts are very slow for some reason, and consequently they appear to get a little clogged up, and keep running after I’ve completed my work. A brief code example:

// called by user
WorkerEventHandler workerDelegate = new WorkerEventHandler(CalcAsync);
workerDelegate.BeginInvoke(p, asyncOp, null, null);

CalcAsync(P p, AsyncOperation asyncOp)
{
   Calculate();
   asyncOp.PostOperationCompleted(new AsyncCompletedEventArgs(null, false, 
      asyncOp.UserSuppliedState);
}

Calculate()
{
   DoStuff()            // updates the progress as it goes
   this.UpdateStatus("Done");   // marks us as done
   this.UpdateProgress(pId, 100);   // increase progress to max
}

When stepping through I get repeated calls to an UpdateProgress being called from the posted events however, so it looks like they’ve been unable to keep up, and are still being posted. These are handled like so:

// initalized with 
//    onProgressUpdatedDelegate = new SendOrPostCallback(UpdateProgress);    
private SendOrPostCallback onProgressUpdatedDelegate; 
public void UpdateProgress(Guid pId, ProgressEventArgs e)
{
   AsyncOperation asyncOp = GetAsyncOperation(pId);

   if (asyncOp != null)
   {
      asyncOp.Post(this.onProgressUpdatedDelegate, e);
   }
   else
   {
      UpdateProgress(this, e);
   }
}

private void UpdateProgress(object state)
{
   ProgressEventArgs e = state as ProgressEventArgs;
   UpdateProgress(this, e); // does the actual triggering of the EventHandler
}

If it’s relevant these events are all writing to a console. But I seem to only see my progress go down on longer operations, which seems to be the same results I see through my WPF test app.

Does it suggest that the Post is just slow? I suppose what I’d really like is every time I call UpdateProgress via the Post I want to purge any existing posts within the queue if this is the case. But I’m not sure this is possible? If it is, is it possible to purge just for these events, as I don’t want to then purge my UpdateStatus event accidentally…

Edit

Some of the missing methods if that makes it easier.

public void UpdateProgress(Guid pId, ProgressEventArgs e)
{
   AsyncOperation asyncOp = GetAsyncOperation(pId);
   if (asyncOp != null)
   {
      asyncOp.Post(this.onProgressUpdatedDelegate, e);
   }
   else
   {
      UpdateProgress(this, e);
   }
}

private void UpdateProgress(object sender, ProgressEventArgs e)
{
   ProgressChangedEventHandler handler;
   lock (progressUpdateEventLock)
   {
      handler = progressUpdateEvent;
   }

   if (handler != null)
      handler(sender, e);
}
  • 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-13T17:47:39+00:00Added an answer on May 13, 2026 at 5:47 pm

    It isn’t that clear what’s going on here. Getting multiple UpdateProgress calls on the UI thread for one UpdateProgress call on the worker thread is definitely bad. Your code as posted should not do this.

    But, yes, the Post() call is not particularly fast. It takes roughly a millisecond for the message loop on the UI thread to pick up the notification, assuming it is not busy doing something else. This is not one millisecond of hard CPU work, it is a delay induced by the Windows plumbing that handles GetMessage(). The thread context switch is part of it, but just a small part.

    This can have notable nasty side-effects. Calling Post() too often, or having the posted delegate doing too much hard work, can seriously affect the UI thread. To the point that it doesn’t get around its regular duties anymore. Because of the delay, you can get there quickly; it only takes 1000 posts per second.

    Of course, it is never necessary to post that often, the human eye cannot perceive updates at a rate any faster than about 25 per second. Doing it more frequently is just wasted effort. But obtaining this ideal update rate is not particularly easy to achieve, unless the worker thread pays specific attention to elapsed time.

    Anyhoo, it is better to leave it up to the client code to tell you how it wants its notifications marshaled. The FileSystemWatcher.SynchronizingObject is a good example of that. If Post() falls apart, that’s one way for the client code to fix the problem. Also take a look at the BackgroundWorker class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 445k
  • Answers 446k
  • 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 In this instance I was able to isolate the problem… May 15, 2026 at 7:05 pm
  • Editorial Team
    Editorial Team added an answer Are you talking about Obfuscating your SQL Server Data? (To… May 15, 2026 at 7:05 pm
  • Editorial Team
    Editorial Team added an answer Have you tried setting a no-cache header? This may work… May 15, 2026 at 7:05 pm

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.