Building Multithreaded application in c# 2017.

This code has two classes (GetListOfPrimesFromBeginning and GetListOfPrimesFromEnd).  The first finds prime numbers from 2 until a specified limit.  The second finds from the specified limit down to a minimum.
Suppose I want to find prime numbers up to 1000000.
I can run either class separately which will do the job.
How can I run both simultaneously to make my program multi threaded.  My unsuccessful attempt is commented out.
My aim is to see if a multi threaded application is quicker.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
namespace FindPrimes
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch s = new Stopwatch();
            s.Start();
            
            GetListOfPrimesFromBeginning a = new GetListOfPrimesFromBeginning();
            a.PrintList(1000000);
            GetListOfPrimesFromEnd b = new GetListOfPrimesFromEnd();
            b.PrintList(1000000, 2);
            //Thread T = new Thread(a.PrintList(100));

            s.Stop();
            Console.WriteLine(s.ElapsedMilliseconds);
            Console.ReadKey();
            //Environment.Exit(0);
        }
    }
}

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace FindPrimes
{
    class GetListOfPrimesFromBeginning
    {
        public void PrintList(int MaxNumberToTry)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(@"D:\Documents\Work\PrimeNumbers\Prime1.csv", false))
                {
                    int CheckThisNumber = 2;
                    int Counter = 2;
                    for (CheckThisNumber = 2; CheckThisNumber <= MaxNumberToTry; CheckThisNumber++)
                    {
                        Counter = 2;
                        //for (Counter = 2; Counter*Counter <= CheckThisNumber; Counter++)
                        do
                        {
                            if (CheckThisNumber % Counter == 0)
                            {
                                break;//CheckThisNumber not prime number
                            }
                            else
                            {

                            }
                            Counter += 1;
                        } while (Counter * Counter < CheckThisNumber);
                        if (Counter * Counter > CheckThisNumber  )
                        {
                            sw.WriteLine(CheckThisNumber);
                            Counter = 2;
                        }

                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }
    class GetListOfPrimesFromEnd
    {
        public void PrintList(int MaxNumberToTry,int MinNumberToTry)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(@"D:\Documents\Work\PrimeNumbers\Prime2.csv", false))
                {
                    int CheckThisNumber = 2;
                    int Counter = 2;
                    for (CheckThisNumber = MaxNumberToTry; CheckThisNumber >= MinNumberToTry; CheckThisNumber--)
                    {
                        Counter = 2;
                        //for (Counter = 2; Counter*Counter <= CheckThisNumber; Counter++)
                        do
                        {
                            if (CheckThisNumber % Counter == 0)
                            {
                                break;//CheckThisNumber not prime number
                            }
                            else
                            {

                            }
                            Counter += 1;
                        } while (Counter * Counter < CheckThisNumber);
                        if (Counter * Counter > CheckThisNumber)
                        {
                            sw.WriteLine(CheckThisNumber);
                            Counter = 2;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }
}

Open in new window

AlHal2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
Hi AIHal2,


Just use Task and wrap your two calls like this snippet:
https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx

Task t1 = Task.Run( () => {
GetListOfPrimesFromBeginning a = new GetListOfPrimesFromBeginning();
            a.PrintList(1000000);
                               } );



Task t2 = Task.Run( () => {
  GetListOfPrimesFromEnd b = new GetListOfPrimesFromEnd();
            b.PrintList(1000000, 2);
                               } );

Open in new window


I wrote this in a notepad, please let me know if you run into errors.

Regards,
Chinmay.
AlHal2Author Commented:
For 1000000 it's 20 the task is 20 times quicker.  Why is that?
Is it to do with what's mentioned in this thread?

More seriously, it works fine for 1,000,000.  However for 10,000,000 the first file seems to be incomplete.
Chinmay PatelChief Technical NinjaCommented:
Can you clarify your first 2 questions a bit more?


For 1000000 it's 20 the task is 20 times quicker.  Why is that?
Is it to do with what's mentioned in this thread?

Can you post your updated code and expected result?
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

AlHal2Author Commented:
The classes above are unchanged.    I've just change static void main.
The uncommented code runs 20 times faster than the commented code for 1,000,000.
It runs many times faster for 10,000,000.  However for 10,000,000 one of the files is incomplete.

        static void Main(string[] args)
        {
            Stopwatch s = new Stopwatch();
            s.Start();

            //GetListOfPrimesFromBeginning a = new GetListOfPrimesFromBeginning();
            //a.PrintList(800000);
            //GetListOfPrimesFromEnd b = new GetListOfPrimesFromEnd();
            //b.PrintList(1000000, 800001);

            Task t1 = Task.Run(() =>
            {
                GetListOfPrimesFromBeginning a = new GetListOfPrimesFromBeginning();
                a.PrintList(8000000);
            });



            Task t2 = Task.Run(() =>
            {
                GetListOfPrimesFromEnd b = new GetListOfPrimesFromEnd();
                b.PrintList(10000000, 8000001);
            });

            s.Stop();
            Console.WriteLine(s.ElapsedMilliseconds);
            Console.ReadKey();
            //Environment.Exit(0);
        }

Open in new window

AlHal2Author Commented:
I should add that in the commented code I also tested 10,000,000 not 1,000,000 as shown.  The thread to which I was referring is
https://stackoverflow.com/questions/13125105/why-so-much-difference-in-performance-between-thread-and-task
Chinmay PatelChief Technical NinjaCommented:
I believe you have understood why Task is faster than manually creating and managing threads by looking at the stackoverflow query.
And about the incomplete file, I need to see your expected response and the generated response. Can you share those files?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlHal2Author Commented:
It seems to work now.  Since I can't reproduce the error, I'll sign it off.  Thanks very much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.