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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:48:54+00:00 2026-05-11T05:48:54+00:00

In this article: http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx The author uses the following method to make thread-safe calls

  • 0

In this article:

http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx

The author uses the following method to make thread-safe calls to a Windows Forms control:

private void SetText(string text) {     // 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;     } } 

Is there a shorter way to accomplish the same thing?

  • 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. 2026-05-11T05:48:55+00:00Added an answer on May 11, 2026 at 5:48 am

    C# 3.0 and after:

    An extension method would generally be the way to go, since you’re always going to want to perform an action on an ISynchronizeInvoke interface implementation, it’s a good design choice.

    You can also take advantage of anonymous methods (closures) to account for the fact that you don’t know what parameters to pass to the extension method; the closure will capture the state of everything needed.

    // Extension method. static void SynchronizedInvoke(this ISynchronizeInvoke sync, Action action) {     // If the invoke is not required, then invoke here and get out.     if (!sync.InvokeRequired)     {         // Execute action.         action();          // Get out.         return;     }      // Marshal to the required context.     sync.Invoke(action, new object[] { }); } 

    You’d then call it like this:

    private void SetText(string text) {     textBox1.SynchronizedInvoke(() => textBox1.Text = text); } 

    Here, the closure is over the text parameter, that state is captured and passed as part of the Action delegate passed to the extension method.

    Before C# 3.0:

    You don’t have the luxury of lambda expressions, but you can still generalize the code. It’s pretty much the same, but not an extension method:

    static void SynchronizedInvoke(ISynchronizeInvoke sync, Action action) {     // If the invoke is not required, then invoke here and get out.     if (!sync.InvokeRequired)     {         // Execute action.         action();          // Get out.         return;     }      // Marshal to the required context.     sync.Invoke(action, new object[] { }); } 

    And then you call it with anonymous method syntax:

    private void SetText(string text) {     SynchronizedInvoke(textBox1, delegate() { textBox1.Text = text; }); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview and have modified it following this article: http://msdn.microsoft.com/en-us/library/aa992036.aspx#Y3473 to allow
I was following this article: http://msdn.microsoft.com/en-us/library/aa902637%28v=sql.80%29.aspx and my query for distinct count looks like
So , I've been reading this article: http://msdn.microsoft.com/en-us/library/aa290051%28VS.71%29.aspx And I would like to define
I've followed the instructions in this MSDN article: http://msdn.microsoft.com/en-us/library/dd206945.aspx Is it possible to call
This example is from the PLINQ MSDN article: http://msdn.microsoft.com/en-us/library/dd997399.aspx var queryA = from num
So I followed MS article http://msdn.microsoft.com/en-us/library/ms171645.aspx This is Creating an Explorer Style Interface with
I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I
As guide I use this article http://msdn.microsoft.com/en-us/library/bb668969.aspx After step 3 (Analyzing sourcesafe folder) I
Reading through this MSDN article http://msdn.microsoft.com/en-us/library/dd460648.aspx I'm seeing a claim that MEF doesn't have
I am trying to create LinqToSql classes based on this article http://msdn.microsoft.com/en-us/library/bb425822.aspx I couldn't

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.