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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:33:12+00:00 2026-05-25T15:33:12+00:00

I’ve got a code block that has to be executed in the same thread

  • 0

I’ve got a code block that has to be executed in the same thread as the window. So I’d like call Form.Invoke for this block. But the method, containing the code block, can be called from different threads and one of them is the window thread.

So my question is: Is it ok to just use Form.Invoke although the method might be called already in the right thread? Or does this generate an overhead or even be a possible source of error?

  • 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-25T15:33:13+00:00Added an answer on May 25, 2026 at 3:33 pm

    Yes it is fine to call Invoke from the thread that owns the handle.

    The overwhelming majority of code samples suggest that you should test InvokeRequired before deciding whether or not to call Invoke.

    if (control.InvokeRequired) 
    {
        control.Invoke(action);
    }
    else
    {    
        action();
    }
    

    However, this leads to rather cluttered code and if you must support calls from non-GUI threads then I recommend that you simply call Invoke directly in all cases. There is a small overhead versus calling InvokeRequired and then the delgate directly – see Hans Passant’s comments to the question. However, so long as the delegate is performing significant work, or this code is not in a hot spot, the overhead should matter less than the code clarity.

    What’s more InvokeRequired can give misleading results in the control’s handle has not yet been allocated. The documentation states:

    If the control’s handle does not yet exist, InvokeRequired searches up
    the control’s parent chain until it finds a control or form that does
    have a window handle. If no appropriate handle can be found, the
    InvokeRequired method returns false.

    This means that InvokeRequired can return false if Invoke is not
    required (the call occurs on the same thread), or if the control was
    created on a different thread but the control’s handle has not yet
    been created.

    In the case where the control’s handle has not yet been created, you
    should not simply call properties, methods, or events on the control.
    This might cause the control’s handle to be created on the background
    thread, isolating the control on a thread without a message pump and
    making the application unstable.

    You can protect against this case by also checking the value of
    IsHandleCreated when InvokeRequired returns false on a background
    thread. If the control handle has not yet been created, you must wait
    until it has been created before calling Invoke or BeginInvoke.
    Typically, this happens only if a background thread is created in the
    constructor of the primary form for the application (as in
    Application.Run(new MainForm()), before the form has been shown or
    Application.Run has been called.

    So I would take the advice from the final quoted paragraph above and replace the risky InvokeRequired code above with:

    control.Invoke(action);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.