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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:48:31+00:00 2026-06-06T06:48:31+00:00

When I want to update controls across threads this is how I generally end

  • 0

When I want to update controls across threads this is how I generally end up doing it:

this.Invoke((MethodInvoker)delegate { SomeProcedure(); });

The suggested way to do it is actually to call the invoker for the specific control you want to update, but 99% of the time the form (i.e. ‘this’ in my example) and the control are going to have been created on the same thread so I really like doing this way for simplicity’s sake.

I was thinking though that it would be nice if I just had a PostSharp aspect to put on top of SomeProcedure that would wrap it in that mess of a statement for me.

And go… (oh yeah, 100 bonus pts to first usable answer 🙂

  • 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-06T06:48:32+00:00Added an answer on June 6, 2026 at 6:48 am

    I haven’t programmed thread access before on WinForms, but I have done it with PostSharp + Silverlight. So with a bit of googling, I’ll give it a shot. No guarantees that it works though!

    [Serializable]
    public class OnGuiThreadAttribute : MethodInterceptionAspect
    {
        private static Control MainControl;
    
        //or internal visibility if you prefer
        public static void RegisterMainControl(Control mainControl) 
        {
            MainControl = mainControl;
        }
    
        public override void OnInvoke(MethodInterceptionArgs eventArgs)
        {
            if (MainControl.InvokeRequired)
                MainControl.BeginInvoke(eventArgs.Proceed);
            else
                eventArgs.Proceed();
        }
    }
    

    And the idea is at the start of your application, register your main/root control with the attribute. Then any method you want to ensure runs on the main thread, just decorate with [OnGuiThread]. If it’s already on the main thread, it just runs the method. If it’s not, it will promote the method call as a delegate to the main thread asynchronously.

    EDIT: I just figured out (it’s late) that you’re asking to use the specific invoking method for the target control you’re using. Assuming that you decorate instance methods on subclasses for your controls:

    [Serializable]
    public class OnGuiThreadAttribute : MethodInterceptionAspect
    {
        public override void OnInvoke(MethodInterceptionArgs eventArgs)
        {
            //you may want to change this line to more gracefully check 
            //if "Instance" is a Control
            Control targetControl = (Control)eventArgs.Instance;
    
            if (targetControl.InvokeRequired)
                targetControl.BeginInvoke(eventArgs.Proceed);
            else
                eventArgs.Proceed();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to update the lastlogin column value .i wrote the code like this
i want update textarea content onclock on a input button for example: <input type=button
What I want: Update all new commits from server with my local repository in
i have ios application with xcdatamodeld, i want update some data so need a
I have developed a windows forms c# application, i just want update items in
I want to update my PHP GUI view when my data source is updated(data
I want to update an erb.html page in rails with information from a webscraping
I want to update an apk file already installed in the device programmatically to
I want to update 50% of the rows in a table, randomly selected. Is
I want to create a list of progress bars and update the list accordingly.

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.