0

I have a construct like this:

private readonly List<Thread> thr = new List<Thread>();

In a class i have a method with one parameter that i want to call threaded.

    public void testthr(object xxx)
    {
     ......
    }

on button click i start a thread

        for (Int32 i = 0; i < textBox8.Lines.Length; i++)
        {

            var thr1 = new Thread(testthr);
            thr1.Start(textBox8.Lines[i].Trim());

            thr.Add(threadz);

        }

How to make a thread with more than one parameter? Like:

    public void testthr(object xxx, string yyy)
    {
     ......
    }

this class in thread start ?

  • 1
    This question is really hard to understand. Please format your code samples properly, and tell us what errors, if any, you're getting from the code you tried that doesn't work. – millimoose Sep 15 '13 at 17:42
  • 1
    Take a look at http://stackoverflow.com/questions/1195896/c-sharp-threadstart-with-parameters – Dennis Ziolkowski Sep 15 '13 at 17:43

1 Answers1

1

If you want to pass multiple values to a thread proc, you need to create an object to contain them. There are several ways to do that. The easiest is probably to use a Tuple:

for (Int32 i = 0; i < textBox8.Lines.Length; i++)
{

    var thr1 = new Thread(testthr);
    var data = new Tuple<string, string>(textBox8.Lines[i].Trim(), "hello");
    thr1.Start(data);

    thr.Add(thr1);

}

public void testthr(object state)
{
    var data = (Tuple<string,string>)state;
    var item1 = data.Item1;
    var item2 = data.Item2;
    ...
}
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351