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

The Archive Base Latest Questions

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

In below sample code, I use lambda function to make 3 threads doing different

  • 0

In below sample code, I use lambda function to make 3 threads doing different things. My goal is make the thread count configurable, so I was thinking using a loop to start threads. But I always got in static function can't call non-static members error. Can the community help me or direct me to a tutorial? Thanks a lot!

My Code:

internal class FeedClient
{
    private static void Main(string[] args)
    {
        int iteration = 10;
        int ranSleepTime = 1000;
        var obj = new MyClass();

        var threads = new Thread[3];

        (threads[0] = new Thread(() =>
                                     {
                                         Random random = new System.Random();
                                         for (int i = 0; i < iteration; i++)
                                         {
                                             obj.MyMethod("my string 1");
                                             Thread.Sleep(random.Next(ranSleepTime));
                                         }
                                     })).Start();

        (threads[1] = new Thread(() =>
                                     {
                                         Random random = new System.Random();
                                         for (int i = 0; i < iteration; i++)
                                         {
                                             obj.MyMethod("my string 2");
                                             Thread.Sleep(random.Next(ranSleepTime));
                                         }
                                     })).Start();

        (threads[2] = new Thread(() =>
                                     {
                                         Random random = new System.Random();
                                         for (int i = 0; i < iteration; i++)
                                         {
                                             obj.MyMethod("my string 3");
                                             Thread.Sleep(random.Next(ranSleepTime));
                                         }
                                     })).Start();

        foreach (Thread thread in threads)
        {
            thread.Join();
        }

        obj.Close(false);

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

Desired look:

for(int i=0;i<3;i++){
    threads[i] = new Thread(func); // func is the lambda function
    threads[i].Start(myData[i]); // myData[] may be a string array
}
  • 1 1 Answer
  • 1 View
  • 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-16T16:35:18+00:00Added an answer on May 16, 2026 at 4:35 pm

    The error message seems to indicate that you are attempting to use an instance member from a static method somewhere. Naturally that is not allowed since a static method does not have a this reference. Here is how I would refactor your code.

    public static void Main()
    {
      string[] myData = GetStringArray();
      int iteration = 10;   
      int ranSleepTime = 1000;   
      var obj = new MyClass();   
      var threads = new Thread[myData.Length];
    
      for (int i = 0; i < threads.Length; i++)
      {
        int captured = i; // This is required to avoid capturing the loop variable.
        threads[i] = new Thread(
          () =>
          {
            var random = new Random(); 
            for (int i = 0; i < iteration; i++) 
            { 
              obj.MyMethod(myData[captured]); 
              Thread.Sleep(random.Next(ranSleepTime)); 
            } 
          });
        threads[i].Start();
      }
    
      foreach (Thread thread in threads)
      {
        thread.Join();
      }
    
      obj.Close(false);
    }
    

    I must mention, however, that creating new threads in an unbounded loop is generally undesirable. If the loop has a tight bound then maybe, but I would have to get a better understanding of the problem before making any further comments regarding this point.

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

Sidebar

Related Questions

I've created some sample code below, and am trying to use a lambda expression
In the below sample code, I have tried to use Nullable(of T) for DateTime
In the sample code below I am dividing by zero which when I step
Below is some sample code. The window does not resize (python3.2, on a Mac,
When I run the sample code below the width of JTextArea is fixed (100px)
I've put together a small code-sample below (Currently in C# 3.5 but would also
I am trying to modify the sample code below. It currently populates a View
I have written grammar for a language (sample code below) //this is a procedure
I have a plot (sample code pasted below) that I am trying to add
I am building html at server side. The code sample is as below. My

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.