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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:35:10+00:00 2026-05-13T16:35:10+00:00

Many times i am using code like this: public static void ExecuteMethod1InThread(string msg) {

  • 0

Many times i am using code like this:

public static void ExecuteMethod1InThread(string msg)
{
    Thread t = new Thread(
        new ThreadStart(
            delegate
            {
                log(msg);
            }));
    t.IsBackground = true;
    t.Start();
    t.Join();
}

If you notice i am calling a method in a separate thread hoping it would be helpful in performance.

But I am wondering if it is possible to give the method name and parameters dynamically and have it execute it rather than creating blocks of code like above for every method call.

I know we could use reflection but it will affect the performance is my understanding.

I have also heard about anonymous delegates but i am not sure how they work.
Just wondering if anyone could enlighten.

  • 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-13T16:35:10+00:00Added an answer on May 13, 2026 at 4:35 pm

    (Read to the bottom for a crucial point!)

    Well, you’re already using an anonymous method. You can keep using C# 2.0 and make the code somewhat shorter like this:

    public static void ExecuteMethod1InThread(string msg)
    {
        Thread t = new Thread(delegate() { log(msg); });
        t.IsBackground = true;
        t.Start();
        t.Join();
    }
    

    Or you could use a lambda expression from C# 3:

    public static void ExecuteMethod1InThread(string msg)
    {
        Thread t = new Thread(() => log(msg));
        t.IsBackground = true;
        t.Start();
        t.Join();
    }
    

    Now you can make this simpler for calling different methods like this:

    public static void ExecuteMethodInThread<T>(Action<T> method, T argument)
    {
        Thread t = new Thread(() => method(argument));
        t.IsBackground = true;
        t.Start();
        t.Join();
    }
    

    You’d then call this with:

    ExecuteMethodInThread(Log, "message");
    ExecuteMethodInThread(DifferentMethod, 10);
    

    (for example).

    Alternatively, you could change it to just take a ThreadStart to start with:

    public static void ExecuteMethodInThread(ThreadStart action)
    {
        Thread t = new Thread(action);
        t.IsBackground = true;
        t.Start();
        t.Join();
    }
    

    and call it like this:

    ExecuteMethodInThread(() => Log("Message"));
    ExecuteMethodInThread(() => DoSomethingElse(20));
    

    This isn’t quite equivalent, however, due to when various expressions are evaluated. In particular, doing this in a loop could cause the problems Eric Lippert describes here.

    Important point

    However, what you’ve currently got will hurt performance. You’re starting a new thread (rather than using the thread-pool) and then you’re waiting for the thread to finish. This is effectively calling the log method synchronously – how can that help performance?

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

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you removing the subviews anywhere? If not, they just… May 13, 2026 at 9:19 pm
  • Editorial Team
    Editorial Team added an answer It seems you are using tablehilo. Anyways, the Generator code… May 13, 2026 at 9:19 pm
  • Editorial Team
    Editorial Team added an answer This was just patched. http://support.microsoft.com/kb/976662 http://msdn.microsoft.com/en-us/library/cc836466(VS.85).aspx May 13, 2026 at 9:19 pm

Related Questions

I'm using SQL Server 2005 Analysis Services and I'm trying to calculate distance inside
I am working in an application that is mostly single-thread, single user. There are
In a recent question on stubbing, many answers suggested C# interfaces or delegates for
I am using System.Timers.Timer class in one of the classes in my application. I
I'm using TPTP to profile some slow running Java code an I came across

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.