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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:44:51+00:00 2026-06-04T22:44:51+00:00

Refer the thread-safe call tutorial at MSDN , have a look at following statments:

  • 0

Refer the thread-safe call tutorial at MSDN, have a look at following statments:

// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired) { 
    SetTextCallback d = new SetTextCallback(SetText);
    this.Invoke(d, new object[] { text });
} else {
    this.textBox1.Text = text;
}

Of course, I’ve used it many times in my codes, and understand a little why to use it.
But I still have some unclear questions about those statements, so anybody help me to find them out, please.

The questions are:

  1. Will the code can run correctly with the statements in the if body only? I tried and seems it just cause the problem if the control is not initialize completely. I don’t know is there more problem?
  2. Which the advantage of calling method directly (else body) instance via invoker? Does it save resource (CPU, RAM) or something?

Thanks!

  • 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-06-04T22:44:53+00:00Added an answer on June 4, 2026 at 10:44 pm
    1. If you always run just the first part of the if statement, it will always be fine, as Invoke already checks if you’re on the UI thread.
      1. The reason you don’t want to do this is that Invoke has to do a a lot of work to run your method, even if you’re already on the right thread. Here’s what it has to do (extracted from the source of Control.cs):
        • Find the marshaling control via an upward traversal of the parent control chain
        • Check if the control is an ActiveX control and, if so, demand unmanaged code permissions
        • Work out if the call needs to be invoked asynchronously to avoid potential deadlock
        • Take a copy of the calling thread’s execution context so the same security permissions will be used when the delegate is finally called
        • Enqueue the method call, then post a message to invoke the method, then wait (if synchronous) until it completes

    None of the steps in the second branch are required during a direct call from the UI thread, as all the preconditions are already guaranteed, so it’s definitely going to be faster, although to be fair, unless you’re updating controls very frequently, you’re very unlikely to notice any difference.

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

Sidebar

Related Questions

Does the following code run on the main thread? Does main queue refer to
[Refer to https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Markers] I followed instructions for creating a GMap sidebar. This is not
Refer to Converting Letters to Numbers In the file test.in.rtf , I have 'abcd'
When I want to refer to the current thread within the environment of a
Refer below code public void acquire(){ synchronized(a){ print(acquire()); try{ //Thread.sleep(5000); synchronized(this){ wait(5000); } print(I
I have the following definition at the top of my .ASCX file: <%@ Control
I have database table represented as text file in the following pattern: 0|ALGERIA|0| haggle.
My thread: public void main_news_thread(MainApplication main) { ThreadPool.QueueUserWorkItem(p => check_news(validrsslist, 0)); } I call
I have the following class: class IndexItem { private String word; private HashMap<String, Integer>
http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx Executes the specified delegate on the thread that owns the control's underlying window

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.