0

I want the for loop to complete before I print the result, in:

        for (int i = 0; i < 5; i++)
        {
            (new System.Threading.Thread(() =>
                {
                    if (TimeTakingMethod())
                    {
                        ++nResult;
                    }
                })).Start();
        }
        Console.WriteLine("Count = " + nResult);

but Console.WriteLine will not wait for those threads to finish because printing is done on the main thread.

If I change it to:

        System.Threading.Thread t = new System.Threading.Thread(() =>
            {
                for (int i = 0; i < 5; i++)
                {
                    (new System.Threading.Thread(() =>
                    {
                        if (TimeTakingMethod())
                        {
                            ++nResult;
                        }
                    })).Start();
                }
            });
        t.Start();
        t.Join();
        Console.WriteLine("Count = " + nResult);

it will still not solve the problem because the nested threads will not be waited for.

Any simple solution to this? Thanks for going through this.

Prashanth Dn
  • 21
  • 1
  • 2
  • 2
    What is the point of using threads like this? you are just waiting until it finishes anyway... – Idos Mar 20 '16 at 13:48
  • Obscure logic? I'd suggest you to explain what are you doing and why? nested threads for no obvious reason – Saleem Mar 20 '16 at 13:51
  • Idos and Saleem, the wait is because the result should be printed only after the loop processing. I used threading there because each iteration of the loop is time taking. The explanation I've given in the question column is a much simplified version of a complex task so this question may make it look like the program doesn't need multithreading. Crashmstr, I agree, thanks for it. The following written on that page helped, along with other answers given on this page: List threads=new List(); //Add your threads to this collection threads.WaitAll(); – Prashanth Dn Mar 20 '16 at 17:29
  • TVOHM and YottaGinneh, Thanks for the answers, it helped. – Prashanth Dn Mar 20 '16 at 17:29

2 Answers2

1

You should store the created threads to control them, I used a List.

        int nResult = 0;
        List<Thread> threads = new List<Thread>();

        for (int i = 0; i < 5; i++)
        {

            Thread thread = new System.Threading.Thread(() =>
            {
                if (TimeTakingMethod())
                {
                    ++nResult;
                }
            });
            thread.Start();
            threads.Add(thread);
        }

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

        Console.WriteLine("Count = " + nResult);
GustavoHennig
  • 181
  • 1
  • 9
0

An example of how you could do this:

int result = 0;
Task.WaitAll(Enumerable.Range(0, 5)
    .Select(index => Task.Factory.StartNew(() =>
    {
        // Do thread things...
        Interlocked.Increment(ref result);
    })).ToArray());

Console.WriteLine(result);

The two important things to note are Task.WaitAll which will cause the program to wait for all tasks to complete before moving on to the WriteLine call.

Also Interlocked.Increment will allow you to safely increment result from any thread.

TVOHM
  • 2,740
  • 1
  • 19
  • 29