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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:38:19+00:00 2026-05-28T07:38:19+00:00

Overview Are there explanations for Control.BeginInvoke() to not execute a delegate that it is

  • 0

Overview

Are there explanations for Control.BeginInvoke() to not execute a delegate that it is passed?

Code Sample

We have adopted the following pattern in our Winforms applications to safely execute UI releated work on the UI thread:

private Control hiddenControl = new Control();

private void uiMethod()
{
  MethodInvoker uiDelegate = new MethodInvoker(delegate()
  {
    Logging.writeLine("Start of uiDelegate");
    //ui releated operations
    childDialog = new ChildDialog();
    childDialow.show();
    Logging.writeLine("End of uiDelegate");
  });

  if (hiddenControl.InvokeRequired)
  {
    Logging.writeLine("Start of InvokeRequired block");
    hiddenControl.BeginInvoke(uiDelegate);
    Logging.writeLine("End of InvokeRequired block");
  }
  else
  {
    uiDelegate();
  }
}

Here, we create a control “hiddenControl” explicitly for the purpose of running delegates on the UI Thread. We never call endInvoke because it’s apparently not required for Control.BeginInvoke and we never need to return a value because our methods just manipulate UI, anyway.

While extremely verbose, this pattern seems to be a relatively well accepted solution. There is, however, some evidence that even this pattern may not work well in all situations.

Observations

I’m not ruling out an application error and blaming WinForms. After all, select probably isn’t broken. I am however at a loss to explain why a delegate may seemingly not run at all.

In our case, we sometimes observe that the “Start of uiDelegate” log message never executes in certain threading scenarios, even though the “Start of InvokeReqiured block” and “End of InvokeRequired block” execute successfully.

It’s been very hard to replicate this behavior because our application is delivered as a DLL; our customers run it in their own applications. Therefore, we can not make any guarantees how or in which thread these methods may be called.

We ruled out UI thread starvation because it is observed that the UI does not lock down. Presumably, if the UI is being updated, then the message pump is operational and available for pulling messages from the message queue and executing their delegates.

Summary

Given this information, is there anything that we can try to make these calls more bullet-proof? As previously mentioned, we have relatively little control over other threads in a given application, and do not control which context these methods are invoked.

What else can affect how delegates successfully passed to Control.BeginInvoke() execute or not?

  • 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-28T07:38:20+00:00Added an answer on May 28, 2026 at 7:38 am

    According to MSDN InvokeRequired can return false even in cases where InvokeRequired should be true – namely in the case that you access InvokeRequired before the Handle of that control/form (or a parent of it) has been created.

    Basically your check is incomplete which leads to the result you see.

    You need to check IsHandleCreated – if that is false then you are in trouble because an Invoke/BeginInvoke would be necessary BUT won’t work robustly since Invoke/BeginInvoke check which thread created Handle to do their magic…

    Only if IsHandleCreated is true you act based on what InvokeRequired returns – something along the lines of:

    if (control.IsHandleCreated)
    {
        if (control.InvokeRequired)
        {
            control.BeginInvoke(action);
        }
        else
        {
            action.Invoke();
        }
    }
    else 
    { 
        // in this case InvokeRequired might lie - you need to make sure that this never happens! 
        throw new Exception ( "Somehow Handle has not yet been created on the UI thread!" );
    }
    

    Thus the following is important to avoid this problem

    Always make sure that the Handle is already created BEFORE the first access on a thread other than the UI thread.

    According to MSDN you just need to reference control.Handle in the UI thread to force it being created – in your code this must happen BEFORE the very first time you access that control/form from any thread that is not the UI thread.

    For other possibilities see the answer from @JaredPar .

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

Sidebar

Related Questions

I have a document that is an overview document within a folder, there are
Overview: I have an array of 20 byte strings that needs to be stored
Is there an overview of all my code projects on google code? What is
Is there a good overview of tcp data path in Linux (2.6, not 2.4
Is there any comprehensive overview somewhere that discusses all the different types of threads
Overview: I have an application that sometimes must make something with celery- and if
Is there a good overview of initWithNibName , awakeFromNib , and viewDidLoad that lays
In visual studio there is the so called 'test view' that provides an overview
Is there an exact overview what has changed in the SP1 for .NET 3.5?
Is there any way to see an overview of what kind of queries are

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.