0

I want to start a number of threads based on the number of threads the computer running the program will have.

I tried doing switches but it seems that it can't end the threads.

This is my Main thread and doesn't work, it says the threads are out of context on the 2nd switch

Is there anything I can add or should I use a different method altogether?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Security.Principal;


namespace Yahtzee_DataMine
{
    class Program
    {
    public static Random diceValue = new Random();
    public static int numberOfDie = new int();
    public static int numberOfSides = new int();
    private static System.Object lockThis = new System.Object();
    public static decimal percent = new decimal();
    public static ConsoleColor oldColor = Console.ForegroundColor;

    static void Main(string[] args)
    {
        while (true)
        {
            getInfo();
            int processorCount = Environment.ProcessorCount;
            Console.WriteLine(processorCount);
            if (processorCount > 7) { processorCount = 7; }
            switch (processorCount)
            {
                case 7:
                    Thread Rolls6 = new Thread(Rolling2);
                    Rolls6.Start();
                    goto case 6;
                case 6:
                    Thread Rolls5 = new Thread(Rolling3);
                    Rolls5.Start();
                    goto case 5;
                case 5:
                    Thread Rolls4 = new Thread(Rolling4);
                    Rolls4.Start();
                    goto case 4;
                case 4:
                    Thread Rolls3 = new Thread(Rolling5);
                    Rolls3.Start();
                    goto case 3;
                case 3:
                    Thread Rolls2 = new Thread(Rolling6);
                    Rolls2.Start();
                    goto case 1;
                case 2:
                case 1:
                    Thread Rolls1 = new Thread(Rolling7);
                    Rolls1.Start();
                    break;
            }
            while (true)
            {
                char quit = quit = Console.ReadKey().KeyChar;
                if (quit == 'q')
                {
                    Console.WriteLine("\rTerminated");
                    break;
                }
            }

            switch (processorCount)
            {
                case 7:
                    Rolls6.abort();
                    goto case 6;
                case 6:
                    Rolls5.abort();
                    goto case 5;
                case 5:
                    Rolls4.abort();
                    goto case 4;
                case 4:
                    Rolls3.abort();
                    goto case 3;
                case 3:
                    Rolls2.abort();
                    goto case 1;
                case 2:
                case 1:
                    Rolls1.abort();
                    break;
              }
        }
    }

    public static void getInfo()
    {
        if (WindowsIdentity.GetCurrent().Owner.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid) == false)
        {
            Console.WriteLine("You Are Not Running With Elevated Administrative Access.");
            Console.WriteLine("Please Restart And Run Program With Administrative Access");
        }
        #region gettingNumberOfDie
        while (true)
        {
            Console.WriteLine("How Many Die Would You Like To Roll? Type Q To Quit");
            String howManyDie = Console.ReadLine();
            try
            {
                int.TryParse(howManyDie, out numberOfDie);
                if (numberOfDie < 2)
                {
                    Console.WriteLine("Please Enter A Integer Greater Than 1");
                    continue;
                }
                break;
            }
            catch
            {

                Console.WriteLine("Please Enter A Number Or Press Q To Quit");
                continue;
            }
        }
        #endregion

        #region gettingNumberOfSides
        while (true)
        {
            Console.WriteLine("How Many Sides Do You Want Each Die To Have?");
            String howManySides = Console.ReadLine();

            try
            {
                int.TryParse(howManySides, out numberOfSides);
                if (numberOfSides < 2)
                {
                    Console.WriteLine("Please Enter A Integer Greater Than 1");
                    continue;
                }
                break;
            }
            catch
            {
                Console.WriteLine("Please Enter A Number Or Press Q To Quit");
                continue;
            }
        }


        #endregion

        #region gettingPercent
        int percentCounter = 1;
        percent = (1m / (numberOfSides));
        decimal percentMultiplier = percent;
        while (percentCounter < numberOfDie)
        {
            percent = percent * percentMultiplier;
            percentCounter++;
        }
        percent = percent * 100;
        #endregion

        Console.WriteLine("With " + numberOfDie + ", " + numberOfSides + " Sided Die, You Have An " + percent + '%' + " Chance of Getting A Yahtzee With Any Given Roll");

        Console.WriteLine("Press Any Key To Commence");
        Console.ReadKey();
    }
    static int seed = Environment.TickCount;
    static readonly ThreadLocal<Random> random = new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref seed)));
    public static int Rand()
    {
        return random.Value.Next(numberOfSides);
    }

    private static void Rolling1()
    {
        Console.WriteLine("Thread1 Started");
        while (true)
        {
            #region rollS
            Stopwatch rollTime = new Stopwatch();
            rollTime.Start();
            UInt64 numberOfRolls = 0;
            while (true)
            {
                numberOfRolls++;
                int counter = 0;
                int[] valuesOfRoll = new int[numberOfDie];

                #region Roll
                while (counter < numberOfDie)
                {
                    valuesOfRoll.SetValue((Rand() + 1), counter);
                    counter++;
                }
                #region isItYahtzee?
                Boolean isItYahtzee = true;
                int counterYaht = 1;
                while (counterYaht < numberOfDie)
                {
                    if (valuesOfRoll[counterYaht] != valuesOfRoll[0])
                    {
                        isItYahtzee = false;
                        counterYaht++;
                        break;
                    }
                    else
                    {
                        counterYaht++;
                        continue;
                    }
                }
                if ((numberOfRolls % 100000000) == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Thread1 has rolled " + numberOfRolls);
                    Console.ForegroundColor = oldColor;
                }
                #endregion
                #region ifYahtzee
                if (isItYahtzee == true)
                {
                    rollTime.Stop();
                    string time = rollTime.Elapsed.ToString();
                    string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
                    string linesA = numberOfDie + "," + numberOfSides + "," + (numberOfDie * numberOfSides) + "," + numberOfRolls + "," + percent + "%" + "," + (percent * numberOfRolls) + "," + time + "," + timeSec + "," + (numberOfRolls / rollTime.Elapsed.TotalSeconds);
                    string linesB = (numberOfRolls).ToString();
                    lock (lockThis)
                    {
                        System.IO.StreamWriter fileA = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\All.txt", true);
                        fileA.WriteLine(linesA);
                        fileA.Close();
                        System.IO.StreamWriter fileB = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", true);
                        fileB.WriteLine(linesB);
                        fileB.Close();
                    }
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread 1");
                    Console.ForegroundColor = oldColor;
                    numberOfRolls = 0;
                    break;
                }

            }

            #endregion
            #endregion
            #endregion
        }
    }
    private static void Rolling2()
    {
        Console.WriteLine("Thread2 Started");
        while (true)
        {
            #region rollS

            Stopwatch rollTime = new Stopwatch();
            rollTime.Start();
            UInt64 numberOfRolls = 0;
            while (true)
            {
                numberOfRolls++;
                int counter = 0;
                int[] valuesOfRoll = new int[numberOfDie];

                #region Roll
                while (counter < numberOfDie)
                {
                    valuesOfRoll.SetValue((Rand() + 1), counter);
                    counter++;
                }

                #region isItYahtzee?
                Boolean isItYahtzee = true;
                int counterYaht = 1;
                while (counterYaht < numberOfDie)
                {
                    if (valuesOfRoll[counterYaht] != valuesOfRoll[0])
                    {
                        isItYahtzee = false;
                        counterYaht++;
                        break;
                    }
                    else
                    {
                        counterYaht++;
                        continue;
                    }

                }
                if ((numberOfRolls % 100000000) == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Thread2 has rolled " + numberOfRolls);
                    Console.ForegroundColor = oldColor;
                }
                #endregion
                #region ifYahtzee
                if (isItYahtzee == true)
                {
                    rollTime.Stop();
                    string time = rollTime.Elapsed.ToString();
                    string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
                    string linesA = numberOfDie + "," + numberOfSides + "," + (numberOfDie * numberOfSides) + "," + numberOfRolls + "," + percent + "%" + "," + (percent * numberOfRolls) + "," + time + "," + timeSec + "," + (numberOfRolls / rollTime.Elapsed.TotalSeconds);
                    string linesB = numberOfRolls.ToString();
                    lock (lockThis)
                    {
                        System.IO.StreamWriter fileA = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\All.txt", true);
                        fileA.WriteLine(linesA);
                        fileA.Close();
                        System.IO.StreamWriter fileB = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", true);
                        fileB.WriteLine(linesB);
                        fileB.Close();
                    }
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread 2");
                    Console.ForegroundColor = oldColor;
                    numberOfRolls = 0;
                    break;

                }

            }

            #endregion
            #endregion
            #endregion
        }
    }
    private static void Rolling3()
    {
        Console.WriteLine("Thread3 Started");
        while (true)
        {
            #region rollS

            Stopwatch rollTime = new Stopwatch();
            rollTime.Start();
            UInt64 numberOfRolls = 0;
            while (true)
            {
                numberOfRolls++;
                int counter = 0;
                int[] valuesOfRoll3 = new int[numberOfDie];

                #region Roll
                while (counter < numberOfDie)
                {
                    valuesOfRoll3.SetValue((Rand() + 1), counter);
                    counter++;
                }
                #region isItYahtzee?
                Boolean isItYahtzee = true;
                int counterYaht = 1;
                while (counterYaht < numberOfDie)
                {
                    if (valuesOfRoll3[counterYaht] != valuesOfRoll3[0])
                    {
                        isItYahtzee = false;
                        counterYaht++;
                        break;
                    }
                    else
                    {
                        counterYaht++;
                        continue;
                    }

                }
                #endregion
                if ((numberOfRolls % 100000000) == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Thread3 has rolled " + numberOfRolls);
                    Console.ForegroundColor = oldColor;
                }

                #region ifYahtzee
                if (isItYahtzee == true)
                {
                    rollTime.Stop();
                    string time = rollTime.Elapsed.ToString();
                    string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
                    string linesA = numberOfDie + "," + numberOfSides + "," + (numberOfDie * numberOfSides) + "," + numberOfRolls + "," + percent + "%" + "," + (percent * numberOfRolls) + "," + time + "," + timeSec + "," + (numberOfRolls / rollTime.Elapsed.TotalSeconds);
                    string linesB = numberOfRolls.ToString();
                    lock (lockThis)
                    {
                        System.IO.StreamWriter fileA = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\All.txt", true);
                        fileA.WriteLine(linesA);
                        fileA.Close();
                        System.IO.StreamWriter fileB = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", true);
                        fileB.WriteLine(linesB);
                        fileB.Close();
                    }
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread 3");
                    Console.ForegroundColor = oldColor;
                    numberOfRolls = 0;
                    break;
                }
            }
            #endregion
            #endregion
            #endregion
        }
    }
    private static void Rolling4()
    {
        Console.WriteLine("Thread4 Started");
        while (true)
        {
            #region rollS

            Stopwatch rollTime = new Stopwatch();
            rollTime.Start();
            UInt64 numberOfRolls = 0;
            while (true)
            {
                numberOfRolls++;
                int counter = 0;
                int[] valuesOfRoll = new int[numberOfDie];
                #region Roll
                while (counter < numberOfDie)
                {
                    valuesOfRoll.SetValue((Rand() + 1), counter);
                    counter++;
                }
                #region isItYahtzee?
                Boolean isItYahtzee = true;
                int counterYaht = 1;
                while (counterYaht < numberOfDie)
                {
                    if (valuesOfRoll[counterYaht] != valuesOfRoll[0])
                    {
                        isItYahtzee = false;
                        counterYaht++;
                        break;
                    }
                    else
                    {
                        counterYaht++;
                        continue;
                    }
                }
                #endregion
                if ((numberOfRolls % 100000000) == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Thread4 has rolled " + numberOfRolls);
                    Console.ForegroundColor = oldColor;
                }
                #region ifYahtzee
                if (isItYahtzee == true)
                {
                    rollTime.Stop();
                    string time = rollTime.Elapsed.ToString();
                    string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
                    string linesA = numberOfDie + "," + numberOfSides + "," + (numberOfDie * numberOfSides) + "," + numberOfRolls + "," + percent + "%" + "," + (percent * numberOfRolls) + "," + time + "," + timeSec + "," + (numberOfRolls / rollTime.Elapsed.TotalSeconds);
                    string linesB = numberOfRolls.ToString();
                    lock (lockThis)
                    {
                        System.IO.StreamWriter fileA = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\All.txt", true);
                        fileA.WriteLine(linesA);
                        fileA.Close();
                        System.IO.StreamWriter fileB = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", true);
                        fileB.WriteLine(linesB);
                        fileB.Close();
                    }
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread 4");
                    Console.ForegroundColor = oldColor;
                    numberOfRolls = 0;
                    break;
                }
            }
            #endregion
            #endregion
            #endregion
        }
    }
    private static void Rolling5()
    {
        Console.WriteLine("Thread5 Started");
        while (true)
        {
            #region rollS
            Stopwatch rollTime = new Stopwatch();
            rollTime.Start();
            UInt64 numberOfRolls = 0;
            while (true)
            {
                numberOfRolls++;
                int counter = 0;
                int[] valuesOfRoll = new int[numberOfDie];

                #region Roll
                while (counter < numberOfDie)
                {
                    valuesOfRoll.SetValue((Rand() + 1), counter);
                    counter++;
                }
                #region isItYahtzee?
                Boolean isItYahtzee = true;
                int counterYaht = 1;
                while (counterYaht < numberOfDie)
                {
                    if (valuesOfRoll[counterYaht] != valuesOfRoll[0])
                    {
                        isItYahtzee = false;
                        counterYaht++;
                        break;
                    }
                    else
                    {
                        counterYaht++;
                        continue;
                    }
                }

                #endregion
                if ((numberOfRolls % 100000000) == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Thread5 has rolled " + numberOfRolls);
                    Console.ForegroundColor = oldColor;
                }
                #region ifYahtzee
                if (isItYahtzee == true)
                {
                    rollTime.Stop();
                    string time = rollTime.Elapsed.ToString();
                    string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
                    string linesA = numberOfDie + "," + numberOfSides + "," + (numberOfDie * numberOfSides) + "," + numberOfRolls + "," + percent + "%" + "," + (percent * numberOfRolls) + "," + time + "," + timeSec + "," + (numberOfRolls / rollTime.Elapsed.TotalSeconds);
                    string linesB = numberOfRolls.ToString();
                    lock (lockThis)
                    {
                        System.IO.StreamWriter fileA = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\All.txt", true);
                        fileA.WriteLine(linesA);
                        fileA.Close();
                        System.IO.StreamWriter fileB = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", true);
                        fileB.WriteLine(linesB);
                        fileB.Close();
                    }
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread 5");
                    Console.ForegroundColor = oldColor;
                    numberOfRolls = 0;
                    break;
                }
            }
            #endregion
            #endregion
            #endregion
        }
    }
    private static void Rolling6()
    {
        Console.WriteLine("Thread6 Started");
        while (true)
        {
            #region rollS
            Stopwatch rollTime = new Stopwatch();
            rollTime.Start();
            UInt64 numberOfRolls = 0;
            while (true)
            {
                numberOfRolls++;
                int counter = 0;
                int[] valuesOfRoll = new int[numberOfDie];

                #region Roll
                while (counter < numberOfDie)
                {
                    valuesOfRoll.SetValue((Rand() + 1), counter);
                    counter++;
                }
                #region isItYahtzee?
                Boolean isItYahtzee = true;
                int counterYaht = 1;
                while (counterYaht < numberOfDie)
                {
                    if (valuesOfRoll[counterYaht] != valuesOfRoll[0])
                    {
                        isItYahtzee = false;
                        counterYaht++;
                        break;
                    }
                    else
                    {
                        counterYaht++;
                        continue;
                    }

                }

                #endregion
                if ((numberOfRolls % 100000000) == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Thread6 has rolled " + numberOfRolls);
                    Console.ForegroundColor = oldColor;
                }
                #region ifYahtzee
                if (isItYahtzee == true)
                {
                    rollTime.Stop();
                    string time = rollTime.Elapsed.ToString();
                    string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
                    string linesA = numberOfDie + "," + numberOfSides + "," + (numberOfDie * numberOfSides) + "," + numberOfRolls + "," + percent + "%" + "," + (percent * numberOfRolls) + "," + time + "," + timeSec + "," + (numberOfRolls / rollTime.Elapsed.TotalSeconds);
                    string linesB = numberOfRolls.ToString();
                    lock (lockThis)
                    {
                        System.IO.StreamWriter fileA = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\All.txt", true);
                        fileA.WriteLine(linesA);
                        fileA.Close();
                        System.IO.StreamWriter fileB = new System.IO.StreamWriter(Directory.GetCurrentDirectory().ToString() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", true);
                        fileB.WriteLine(linesB);
                        fileB.Close();
                    }
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread 6");
                    Console.ForegroundColor = oldColor;
                    numberOfRolls = 0;
                    break;

                }

            }

            #endregion
            #endregion
            #endregion
        }
    }
  • Have a look at why `abort` is tricky.. http://stackoverflow.com/questions/1559255/whats-wrong-with-using-thread-abort – Orel Eraki Feb 07 '16 at 21:26
  • what does method Rolling1 (and the others) do? – alexm Feb 07 '16 at 22:25
  • It rolls a number of die till it gets all of the same number. – DominiUmbra Feb 08 '16 at 00:07
  • What do you suggest I do to stop the threads? – DominiUmbra Feb 08 '16 at 00:07
  • One of the very worst things you can do in your code is call `Thread.Abort()`. It can leave the .NET process in an undefined state. The only time ever that you should call it is if you are trying to force your app to close. I think that you need to show us the rest of your code so that we can help you to do what you need in a safe manner. – Enigmativity Feb 08 '16 at 00:08
  • I added my entire program except for 7 thread due to character limits, but it's the same format as the others. – DominiUmbra Feb 08 '16 at 00:47
  • @DominiUmbra It looks like you are new to C#, my best advice would be to get the basics down before you start delving into multi-threading. You could reuse a single `Rolling(int id)` method rather than copy/pasting the same code over and over. Also look into using a `List` to keep track of your various rolling threads so you are not having to use any `switch` statements at all. You also have countless error-prone cases, what if `int.TryParse` fails? There are race conditions on setting the console color and actually writing. – Lukazoid Feb 08 '16 at 01:02
  • @Lukazoid Ok, thanks for the input, I'm going to start looking into list – DominiUmbra Feb 08 '16 at 01:22

1 Answers1

1

It really looks like you need to learn some more of the basics of C# before you try something this complicated. You've got an interesting mix of really clever and very basic code. It appears that you've copy-and-pasted a lot from elsewhere.

The basic issue you're having is that you're declaring your thread variables (Thread Rolls6 = new Thread(Rolling2);) within the scope of the first switch (processorCount) statement. When it comes to the second one those variables don't exist. You could simply move the variable declarations higher up in the method to make them visible to both, but that's going to be a mistake. Calling Thread.Abort is a very bad practice. You really need something that lets the thread terminate normally.

In a nutshell your threads should look to see when they should end and they should respond accordingly.

The basic way this code looks is this:

private static void Rolling(CancellationToken ct)
{
    while (true)
    {
        if (ct.IsCancellationRequested)
        {
            Console.WriteLine("Done with thread " + n);
            break;
        }
        /* Do Stuff Here, But Let The Code Loop Back */
    }
}

The code keeps checking the CancellationToken to see if IsCancellationRequested has been set or not.

That's how to end a thread cleanly.

Now I have gotten rid of all of those Rolling{n} methods and replaced them with a single method with the signature void Rolling(int n, int numberOfDie, int numberOfSides, double percent, CancellationToken ct). I've tried to get rid of global variables (which are generally bad) and I'm following the CancellationToken pattern.

To start up the threads I've gotten of the switch statements. The thread creation now looks like this:

        CancellationTokenSource[] ctss =
            Enumerable
                .Range(1, processorCount)
                .Select(n =>
                {
                    var cts = new CancellationTokenSource();
                    var t = new Thread(() =>
                        Rolling(n, numberOfDie, numberOfSides, percent, cts.Token));
                    t.Start();
                    return cts;
                })
                .ToArray();

To cancel all of the threads the code just becomes:

        foreach (var cts in ctss)
        {
            cts.Cancel();
        }

...and then the ct.IsCancellationRequested becomes true in the Rolling method and they will shut down themselves.

That's the approach that you should be taking.

Here's your full code:

private static System.Object lockThis = new System.Object();
public static ConsoleColor oldColor = Console.ForegroundColor;
private static int seed = Environment.TickCount;
private static readonly ThreadLocal<Random> random = new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref seed)));

static void Main(string[] args)
{
    var x = GetValue("How Many Die Would You Like To Roll?", 1);
    if (x.HasValue)
    {
        var y = GetValue("How Many Sides Do You Want Each Die To Have?", 2);
        if (y.HasValue)
        {
            int numberOfDie = x.Value;
            int numberOfSides = y.Value;
            double percent = 100 * Math.Pow(1.0 / numberOfSides, numberOfDie);

            Console.WriteLine("With " + numberOfDie + ", " + numberOfSides + " Sided Die, You Have An " + percent + '%' + " Chance of Getting A Yahtzee With Any Given Roll");

            Console.WriteLine("Press Any Key To Commence");
            Console.ReadLine();

            int processorCount = System.Math.Min(Environment.ProcessorCount, 8);
            Console.WriteLine(processorCount);

            CancellationTokenSource[] ctss =
                Enumerable
                    .Range(1, processorCount)
                    .Select(n =>
                    {
                        var cts = new CancellationTokenSource();
                        var t = new Thread(() =>
                            Rolling(n, numberOfDie, numberOfSides, percent, cts.Token));
                        t.Start();
                        return cts;
                    })
                    .ToArray();

            while (true)
            {
                string quit = Console.ReadLine().Substring(0, 1).ToUpper();
                if (quit == "Q")
                {
                    Console.WriteLine(Environment.NewLine, "Terminated");
                    break;
                }
            }

            foreach (var cts in ctss)
            {
                cts.Cancel();
            }
        }
    }
}

private static int? GetValue(string prompt, int minimum)
{
    while (true)
    {
        Console.WriteLine(prompt + " Type Q To Quit");
        var input = Console.ReadLine().Substring(0, 1).ToUpper();
        if (input == "Q")
        {
            return null;
        }
        int output;
        if (int.TryParse(input, out output))
        {
            if (output < minimum)
            {
                Console.WriteLine("Please Enter an Integer Greater Than Or Equal To " + minimum);
                continue;
            }
            else
            {
                return output;
            }
        }
    }
}

private static void Rolling(int n, int numberOfDie, int numberOfSides, double percent, CancellationToken ct)
{
    Console.WriteLine("Thread" + n + " Started");

    Stopwatch rollTime = Stopwatch.StartNew();

    long numberOfRolls = 0;
    while (true)
    {
        if (ct.IsCancellationRequested)
        {
            Console.WriteLine("Done with thread " + n);
            break;
        }

        int[] valuesOfRoll =
            Enumerable
                .Range(0, numberOfDie)
                .Select(x => random.Value.Next(numberOfSides) + 1)
                .ToArray();

        Boolean isItYahtzee = valuesOfRoll.All(x => x == valuesOfRoll[0]);

        if ((numberOfRolls++ % 100000000) == 0)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Thread" + n + " has rolled " + numberOfRolls);
            Console.ForegroundColor = oldColor;
        }

        if (isItYahtzee == true)
        {
            rollTime.Stop();
            string time = rollTime.Elapsed.ToString();
            string timeSec = rollTime.Elapsed.TotalSeconds.ToString();
            string linesA =
                String.Format(
                    "{0},{1},{2},{3},{4}%,{5},{6},{7},{8}",
                    numberOfDie,
                    numberOfSides,
                    numberOfDie * numberOfSides,
                    numberOfRolls,
                    percent,
                    percent * numberOfRolls,
                    time,
                    timeSec,
                    numberOfRolls / rollTime.Elapsed.TotalSeconds);
            string linesB = (numberOfRolls).ToString();
            lock (lockThis)
            {
                File.AppendAllLines(Directory.GetCurrentDirectory() + "\\All.txt", new[] { linesA });
                File.AppendAllLines(Directory.GetCurrentDirectory() + "\\Avg_" + numberOfDie + "X" + numberOfSides + ".txt", new[] { linesB });
            }
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(numberOfRolls + " File Has Been Successfully Save By Thread " + n);
            Console.ForegroundColor = oldColor;
            numberOfRolls = 0;
            break;
        }
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Sorry for not responding sooner, I had a busy week, Thanks for responding so thoroughly and going above and beyond. I am new to programming and I am happy to take your helpful criticism to help me continue learning c# – DominiUmbra Feb 20 '16 at 21:24