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

  • Home
  • SEARCH
  • 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 996785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:56:02+00:00 2026-05-16T06:56:02+00:00

Quite often in my GUI code, I write something like this: private void SecondTimer_Elapsed(object

  • 0

Quite often in my GUI code, I write something like this:

private void SecondTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    if (progressBar1.InvokeRequired)
    {
      progressBar1.BeginInvoke(new ElapsedEventHandler(SecondTimer_Elapsed), new[] {sender, e});
      return;
    }
//Code goes here
}

Of course, this is necessary if the app is multithreaded, as we need to marshall the thread that originally created the control. The thing is, it can be tedious to write the delegate and put the arguments into an array, and it takes up space in the top of every such event handler. Is there an attribute or something along those lines that will replace this code for you? Basically a label that says “if you’re on the wrong thread, call me again on the GUI thread with the same args.”

  • 1 1 Answer
  • 2 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-16T06:56:02+00:00Added an answer on May 16, 2026 at 6:56 am

    I don’t know of anything quite like that, but this would probably be a useful extension method for you:

    public static class Extensions
    {
        public static void Execute(this ISynchronizeInvoke invoker,
                                   MethodInvoker action)
        {
            if (invoker.InvokeRequired)
            {
                 invoker.BeginInvoke(action);
            }
            else
            {
                 action();
            }
        }
    }
    

    Now this only works for parameterless delegates, of course… but with lambda expressions that needn’t be a problem:

    progressBar1.Execute(() => SecondTimer_Elapsed(sender, e));
    

    This has the following advantages:

    • It’s simple to write
    • You can’t go wrong with the signature (you’re not relying on late binding)
    • MethodInvoker is executed slightly more efficiently than other delegates, I believe
    • If you don’t have much code to execute, you can write it inline in the lambda expression
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quite often see in JavaScript libraries code like this: setTimeout(function() { ... }, 0);
I bump into something like this quite often. This time I have sprite sheet
Preamble Using VTK library with C++, quite often I have to write something like
I quite often have rows of code like the following: UPDATE my_table SET name
I find myself adding debugging print statements quite often -- stuff like this: print(a_variable_name:
I quite often write code that copies member variables to a local stack variable
I've see this sort of thing in Java code quite often... try { fileStream.close();
Quite often, I find myself wanting a simple, dump object in Python which behaves
I quite often see the following naming convention used in java code. class SomeClass
I ran into this quite often stated problem but even after looking up nearly

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.