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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:32:28+00:00 2026-05-14T02:32:28+00:00

I’ve inherited code where BeginInvoke is called from the main thread (not a background

  • 0

I’ve inherited code where BeginInvoke is called from the main thread (not a background thread, which is usually the pattern). I am trying to understand what it actually does in this scenario.

Does the method being called in the BeginInvoke get in line of messages that come down to the window? The docs say asynchronously, so that is my assumption.

How does the framework prioritize when to kick off the method called by BeginInvoke?

Edit: The code looks like this:

System.Action<bool> finalizeUI = delegate(bool open)
{
    try
    {
        // do somewhat time consuming stuff
    }
    finally
    {
        Cursor.Current = Cursors.Default;
    }
};

Cursor.Current = Cursors.WaitCursor;
BeginInvoke(finalizeUI, true);

This is happening in the Form_Load event.

  • 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-14T02:32:29+00:00Added an answer on May 14, 2026 at 2:32 am

    edit

    Now that we see the code, it’s clear that this is just a way to move some initialization out of Form_Load but still have it happen before the user can interact with the form.

    The call to BeginInvoke is inside Form_load, and is not called on another object, so this is a call to Form.BeginInvoke. So what’s happening is this.

    1. Form_Load passes a delegate to Form.BeginInvoke, this puts a message in the form’s message queue that is ahead of all user input messages. It sets the cursor to a wait cursor.
    2. Form_Load returns, and the rest of form initialization is allowed to complete, the form most likely becomes visible at this point.
    3. Once the code falls into the message pump, the first thing is sees in the queue is the delegate, so it runs that.
    4. as the delegate completes, it changes the cursor back to the normal cursor, and returns
    5. profit!

    original post below


    I depends on the object that you call BeginInvoke on. If the object is derived from Control then Control.BeginInvoke will run on the thread that created the control. See JaredPar’s answer.

    But there is another pattern for the use of BeginInvoke. if the object is a delegate, then BeginInvoke runs the callback on a separate thread, one that may be created specifically for that purpose.

    public class Foo
    {
        ...
        public Object Bar(object arg)
        {
           // this function will run on a separate thread.
        }
    }
    
    ...
    
    // this delegate is used to Invoke Bar on Foo in separate thread, this must
    // take the same arguments and return the same value as the Bar method of Foo
    public delegate object FooBarCaller (object arg);
    
    ...
    
    // call this on the main thread to invoke Foo.Bar on a background thread
    //
    public IAsyncResult BeginFooBar(AsyncCallback callback, object arg)
    {
       Foo foo = new Foo();
       FooBarCaller caller = new FooBarCaller (foo.Bar);
       return caller.BeginInvoke (arg);
    }
    

    This pattern is one reason that BeginInvoke is called from the main thread rather than from a background thread.

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

Sidebar

Ask A Question

Stats

  • Questions 341k
  • Answers 341k
  • 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 You can modify __call__ function in BaseController. May 14, 2026 at 4:57 am
  • Editorial Team
    Editorial Team added an answer Use the GetPrintCapabilities() method of the PrintQueue and then query… May 14, 2026 at 4:57 am
  • Editorial Team
    Editorial Team added an answer Are you using a version of Java older than 1.5?… May 14, 2026 at 4:57 am

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

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.