开发者

C# using loop to start threads and pass parameters

开发者 https://www.devze.com 2023-01-15 09:56 出处:网络
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 i

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
}


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号