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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:23:15+00:00 2026-05-24T07:23:15+00:00

What exactly does Control.Invoke(Delegate) do to get the delegate to run on the GUI

  • 0

What exactly does Control.Invoke(Delegate) do to get the delegate to run on the GUI thread? Furthermore, Its my understanding that invoke will block until the invoked function its done. How does it achieve this?

I would like some good gritty details. I’m hoping to learn something interesting.

  • 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-24T07:23:17+00:00Added an answer on May 24, 2026 at 7:23 am

    Edit: The control implements ISynchronizeInvoke interface, You can make the same effect using the SynchronizationContext and call Post when you call Invoke. something like:

    public object Invoke(Delegate method, object[] args)
    {
        if (method == null)
        {
            throw new ArgumentNullException("method");
        }
    
        object objectToGet = null;
    
        SendOrPostCallback invoker = new SendOrPostCallback(
        delegate(object data)
        {
            objectToGet = method.DynamicInvoke(args);
        });
    
        _currentContext.Send(new SendOrPostCallback(invoker), method.Target);
    
        return objectToGet;
    }
    

    Further investigation using Reflector shows that the Invoke uses some native API calls to achieve that:

    private object MarshaledInvoke(Control caller, Delegate method, object[] args, bool synchronous)
    {
        int num;
        if (!this.IsHandleCreated)
        {
            throw new InvalidOperationException(SR.GetString("ErrorNoMarshalingThread"));
        }
        if (((ActiveXImpl) this.Properties.GetObject(PropActiveXImpl)) != null)
        {
            IntSecurity.UnmanagedCode.Demand();
        }
        bool flag = false;
        if ((SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(this, this.Handle), out num) == SafeNativeMethods.GetCurrentThreadId()) && synchronous)
        {
            flag = true;
        }
        ExecutionContext executionContext = null;
        if (!flag)
        {
            executionContext = ExecutionContext.Capture();
        }
        ThreadMethodEntry entry = new ThreadMethodEntry(caller, this, method, args, synchronous, executionContext);
        lock (this)
        {
            if (this.threadCallbackList == null)
            {
                this.threadCallbackList = new Queue();
            }
        }
        lock (this.threadCallbackList)
        {
            if (threadCallbackMessage == 0)
            {
                threadCallbackMessage = SafeNativeMethods.RegisterWindowMessage(Application.WindowMessagesVersion + "_ThreadCallbackMessage");
            }
            this.threadCallbackList.Enqueue(entry);
        }
        if (flag)
        {
            this.InvokeMarshaledCallbacks();
        }
        else
        {
            UnsafeNativeMethods.PostMessage(new HandleRef(this, this.Handle), threadCallbackMessage, IntPtr.Zero, IntPtr.Zero);
        }
        if (!synchronous)
        {
            return entry;
        }
        if (!entry.IsCompleted)
        {
            this.WaitForWaitHandle(entry.AsyncWaitHandle);
        }
        if (entry.exception != null)
        {
            throw entry.exception;
        }
        return entry.retVal;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found this amazing control that does exactly what I need, only problem,
Does someone knows of a user control that can work exactly like the file
What exactly does immutable mean - that is, what are the consequences of an
What exactly does a mobile need to be able to run JavaFX? Can it
What exactly does a finally block in exception handling perform?
What does that method do exactly? I was thinking that it maybe went out
I have a service application built in Delphi that works great. It does exactly
I know, that views are compiled on the fly, but when exactly does it
I have one main thread that does some rather CPU intensive operation. The thread
What exactly does the word patch mean when referring to 'submitting a patch'? I've

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.