-2

I have a c# windows form app. Right now it works fine alone in single thread. Here is how I launch it in a click on button.

botThread = new Thread(new ThreadStart(mainThread));
botThread.Start();

So one thread gets launched.

The user will specify the number of threads to run at a time in a text box as number. So I want that many threads to get launched and each thread should get a line from a text file. so if the thread is first, it should get the first line of a text file.

If a user specified two threads, once the first instance of thread ends doing the job or it had any expection, i want the bot to move to the next instance and start a new thread with the 3rd line of text file.

I am not sure what to do. I tried using for loop but i couldn't keep track of the threads and couldn't launch new ones when one ends

edit : Here is for loop code :

int j = int.Parse(settings_textbox_instances.Text);
            for (i = 0; i < j; i++)
            {
                botThread = new Thread(new ThreadStart(mainThread));
                botThread.Start();

            }

edit 1 :

I have worked with the code and made this.

private void multiThreader(int instances)
        {

            for (i = 0; i < instances; i++)
            {
                botThread = new Thread(new ThreadStart(mainThread));
                string text = this.users_List[this.i];


                botThread.Start();

                threadcount++;
                MessageBox.Show(i.ToString());

            } 
        }

so when ending the thread i can call the void in to create new instances. But can anyone say to me on how i can transfer the value of text from here to the thread ?

3 Answers3

1

Do you really need to allow the user to manage the number of threads?

You can let the framework take care of the detail and use the Task Parallel Library.

Here is a simple example of usage...

 static void Main()
{
    var lines = System.IO.File.ReadAllLines(@"c:\temp\log.txt");
    Parallel.ForEach(lines,line => processLine(line));
}

private static void processLine(string line)
{
    Console.WriteLine(line);
}

As well as the TPL documentation linked above, i would highly recommend reading Joe Albaharis excellent series on Threading in C# to gain a richer understanding of the concepts.

Baldy
  • 3,621
  • 4
  • 38
  • 60
1

You could instead use Parallel.ForEach

        var l = new List<string>();
        using (var fs = new FileStream("", FileMode.Open, FileAccess.Read))
        {
            using (var sr = new StreamReader(fs))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    l.Add(line);
                }
            }
        }
        Parallel.ForEach(l, new ParallelOptions {MaxDegreeOfParallelism = 10}, line => Console.WriteLine(line));

for clarity

        Parallel.ForEach(l, new ParallelOptions {MaxDegreeOfParallelism = 10}, MyAction);

    private void MyAction(string s)
    {
        throw new NotImplementedException();
    }

Where MaxDegreeOfParallelism would be the number provided by the textbox.

woutervs
  • 1,500
  • 12
  • 28
  • it is not a console. My main problem here is lauching the threads multiple times – user3502927 Apr 09 '14 at 09:40
  • the console.writeline is only there to have an action, you can specifiy your own action to be wathever processing needs done on the string... see my edit. – woutervs Apr 09 '14 at 09:42
0

you can use on your code this function to see when you start your another thread :

//return true if the thread is alive else false
botThread.IsAlive



int j = int.Parse(settings_textbox_instances.Text);
            for (i = 0; i < j; i++)
            {
                botThread = new Thread(new ThreadStart(mainThread));
                botThread.Start();
                while (botThread.IsAlive)
                {}
                botThread2 = new Thread(new ThreadStart(mainThread));
                botThread2.Start();
            }
mister
  • 95
  • 6
  • as the previous one process is alive he will not go to the next process. the while loop ensures that the process is still alive. – mister Apr 09 '14 at 09:50
  • you can use a global variable and put the line of your text. and you start in the next line when you done your processing. – mister Apr 09 '14 at 09:50
  • i have came up with a idea and updated my question. Can you help me to say how to transfer the text from my fucntion to thread. – user3502927 Apr 09 '14 at 09:52
  • sorry I dont know about the question second part – mister Apr 09 '14 at 11:09
  • But that has been asked before, see [here](http://stackoverflow.com/questions/1195896/threadstart-with-parameters). – ThaMe90 Apr 09 '14 at 11:51