1 Replies - 609 Views - Last Post: 09 July 2016 - 05:32 PM Rate Topic: -----

#1 te777   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-February 13

Problem with multi-threading in Visual C#

Posted 09 July 2016 - 03:28 PM

I have a test class that generates 2 arrays of random numbers, then checks for equality between the rows of the arrays using a Task Array. The problem is the output. It shows all zeroes except for the last line, when there should show some formatted output. I think there might be a race condition in the code, or some other bug. I'm new to using Tasks, so forgive me if the answer might be obvious. I'm posting the entire class so someone can run the code if they wish. I hope it's not too much for a forum. ANy replies would be welcome.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    class TestObjPass
    {
        public const int MAX = 1000;
        int[,] nums = new int[MAX + 1, 6];
        int[,] test_nums = new int[MAX + 1, 6];
        public string testPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Test";

       // Combos_check_anaylze_results[] test_results = new Combos_check_anaylze_results[MAX + 1];

        public void analyze()
        {
            random1();
            random2();

            Combos_check_anaylze_results[] test_results = new Combos_check_anaylze_results[MAX + 1];

            System.Threading.Tasks.Task[] taskArray = new System.Threading.Tasks.Task[MAX];

            int index = 0;

            for (int i = 0; i < taskArray.Length; i++)
            {
              
                index++;
                
                test_results[index] = new Combos_check_anaylze_results();

                taskArray[i] = System.Threading.Tasks.Task.Factory.StartNew((Object obj) =>
                {
                    analyze_task( index, ref test_results[index]);
                },
             i);

            }
            System.Threading.Tasks.Task.WaitAll(taskArray);

            analyze_output(ref test_results);            

        }

        public void analyze_task(int index, ref Combos_check_anaylze_results result)
        {
            int[] check_nums = new int[6];

            for (int j = 1; j <= 5; j++)
            {
                check_nums[j] = nums[index, j];
            }

            combos_check_analyze(check_nums, ref result);


        }

        public void random1()
        {
             
            Random rnd = new Random();
        
            for (int ctr = 1; ctr <= MAX; ctr++)
            {
               
                for (int i = 1; i <= 5; i++)
                {
                    nums[ctr, i] = rnd.Next(1, 76);

                    for (int j = i - 1; j >= 1; j--)
                    {
                        if (nums[ctr, i] == nums[ctr, j])
                        {
                            nums[ctr, i] = rnd.Next(1, 76);
                        }


                    }

                }

            }
        }

        public void random2()
        {
          
            Random rnd = new Random();

            for (int ctr = 1; ctr <= MAX; ctr++)
            {

                for (int i = 1; i <= 5; i++)
                {
                    test_nums[ctr, i] = rnd.Next(1, 76);

                    for (int j = i - 1; j >= 1; j--)
                    {
                        if (test_nums[ctr, i] == test_nums[ctr, j])
                        {
                            test_nums[ctr, i] = rnd.Next(1, 76);
                        }


                    }

                }

            }
        }

        public void combos_check_analyze( int[] check_nums,ref Combos_check_anaylze_results result)
        {
            string Temp;
            string[] Temp2 = new string[20];
            char[] delimitChars = { ' ' };
            int i, j, k, l;
            //int[] check_nums = new int[7];

            int[] matches = new int[6];
            int matches_line = 1;


            bool matches_2_found;
            bool matches_3_found;
            bool matches_4_found;
            bool matches_5_found;

            int matches_2_count;
            int matches_3_count;
            int matches_4_count;
            int matches_5_count;

            int matches_count;

            matches_2_found = false;
            matches_3_found = false;
            matches_4_found = false;
            matches_5_found = false;

            matches_2_count = 0;
            matches_3_count = 0;
            matches_4_count = 0;
            matches_5_count = 0;

            for (i = 1; i <= MAX; i++)
            {

                matches_count = 0;

                for (k = 1; k <= 5; k++)
                {

                    for (l = 1; l <= 5; l++)
                    {

                        if (check_nums[k] == test_nums[i, l])
                        {

                            matches_count += 1;

                        }

                    }

                }

                if (matches_count == 2 && matches_2_count == 0)
                {

                    matches_2_found = true;
                    matches_2_count += 1;

                    if (matches_2_count == 1)
                    {

                        matches_line = i;

                        for (j = 1; j <= 5; j++)
                        {

                            matches[j] = test_nums[i, j];

                        }

                    }

                }

                else if (matches_count == 3 && matches_3_count == 0)
                {

                    matches_3_found = true;
                    matches_3_count += 1;

                    if (matches_3_count == 1)
                    {

                        matches_line = i;

                        for (j = 1; j <= 5; j++)
                        {

                            matches[j] = test_nums[i, j];

                        }

                    }

                }

                else if (matches_count == 4 && matches_4_count == 0)
                {
                    matches_4_found = true;
                    matches_4_count += 1;

                    if (matches_4_count == 1)
                    {

                        matches_line = i;

                        for (j = 1; j <= 5; j++)
                        {

                            matches[j] = test_nums[i, j];

                        }

                    }


                }

                else if (matches_count == 5 && matches_5_count == 0)
                {
                    matches_5_found = true;
                    matches_5_count += 1;

                    if (matches_5_count == 1)
                    {

                        matches_line = i;

                        for (j = 1; j <= 5; j++)
                        {

                            matches[j] = test_nums[i, j];

                        }

                    }


                }


            }


            if (matches_5_count > 0)
            {
                matches_2_found = false;
                matches_3_found = false;
                matches_4_found = false;

            }

            else if (matches_4_count > 0)
            {
                matches_2_found = false;
                matches_3_found = false;
                matches_5_found = false;
            }

            else if (matches_3_count > 0)
            {
                matches_2_found = false;
                matches_4_found = false;
                matches_5_found = false;
            }

            else if (matches_2_count > 0)
            {
                matches_3_found = false;
                matches_4_found = false;
                matches_5_found = false;
            }


            if (matches_2_found)
            {
                result.line1 = " Numbers ";
                result.found = 2;
                result.line2 = " found ";
                for (j = 1; j <= 5; j++)
                {
                    result.numbers[j] = matches[j];
                }
                result.line = " line  ";
                result.line_number = matches_line;

                
            }
            else if (matches_3_found)
            {
                result.line1 = " Numbers ";
                result.found = 3;
                result.line2 = " found ";
                for (j = 1; j <= 5; j++)
                {
                    result.numbers[j] = matches[j];
                }
                result.line = " line  ";
                result.line_number = matches_line;

               
            }
            else if (matches_4_found)
            {
                result.line1 = " Numbers ";
                result.found = 4;
                result.line2 = " found ";
                for (j = 1; j <= 5; j++)
                {
                    result.numbers[j] = matches[j];
                }
                result.line = " line  ";
                result.line_number = matches_line;

                
            }
            else if (matches_5_found)
            {
                result.line1 = " Numbers ";
                result.found = 5;
                result.line2 = " found ";
                for (j = 1; j <= 5; j++)
                {
                    result.numbers[j] = matches[j];
                }
                result.line = " line  ";
                result.line_number = matches_line;

               
            }
            

            else
            {
                result.line1 = " Numbers ";
                result.found = 1;
                result.line2 = " found ";
                for (j = 1; j <= 5; j++)
                {
                    result.numbers[j] = matches[j];
                }
                result.line = " line  ";
                result.line_number = matches_line;

               
            }


        }

        public void analyze_output(ref Combos_check_anaylze_results[] result)
        {
            string path;
            string Temp;
            Single line_number_avg;
            Single count_number_avg;

            if (!System.IO.Directory.Exists(testPath))
                System.IO.Directory.CreateDirectory(testPath);

            
            path = testPath + "\\randomtest.txt";

            System.IO.StreamWriter outFile = new System.IO.StreamWriter(path);

            line_number_avg = 0;
            count_number_avg = 0;

            for (int i = 1; i <= MAX; i++)
            {
                Temp = i.ToString();

                if (i < 10)

                    Temp += " ";

                if (i < 100)

                    Temp += " ";

                if (i < 1000)

                    Temp += " ";

                if (i < 10000)

                    Temp += " ";

                if (i < 100000)

                    Temp += " ";

                Temp += result[i].line1;

                Temp += result[i].found.ToString();

                count_number_avg += result[i].found;

                Temp += result[i].line2;

                for (int j = 1; j <= 5; j++)
                {

                    if (result[i].numbers[j] < 10)

                        Temp += " ";

                    Temp += "  " + result[i].numbers[j].ToString();
                }

                Temp += result[i].line;

                if (result[i].line_number < 10)

                    Temp += " ";

                if (result[i].line_number < 100)

                    Temp += " ";

                if (result[i].line_number < 1000)

                    Temp += " ";

                if (result[i].line_number < 10000)

                    Temp += " ";

                if (result[i].line_number < 100000)

                    Temp += " ";


                Temp += result[i].line_number.ToString(); ;

                line_number_avg += result[i].line_number;

                outFile.WriteLine(Temp);
            }

            line_number_avg = line_number_avg / (Single)MAX;
            count_number_avg = count_number_avg / (Single)MAX;

            outFile.WriteLine("\r\n" + "Line number average = " + line_number_avg.ToString());

            outFile.WriteLine("\r\n" + "Count number average = " + count_number_avg.ToString());

            outFile.Close();

            System.Diagnostics.Process.Start("Notepad.exe", path);

        }

    }




I forgot a needed extra small class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

   class Combos_check_anaylze_results
    {
       
        public string line1;
        public int found;
        public string line2;
        public int[] numbers = new int[6];
        public string line;
        public int line_number;

    }



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with multi-threading in Visual C#

#2 te777   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-February 13

Re: Problem with multi-threading in Visual C#

Posted 09 July 2016 - 05:32 PM

I found the problem. In the following line

analyze_task( index, ref test_results[index]);



There was a problem with the counter variable "index" being passed to the that task in the task array. I set the values for the parameters before passing and it fixed the problem;

In the analyze method, the following changes:

for (int i = 0; i < taskArray.Length; i++)
{

	index = index + 1; 

        int[] check_nums = new int[6];

        for (int j = 1; j <= 5; j++)
                {
                    check_nums[j] = nums[index, j];
        }


        test_results[index] = new Combos_check_analyze_results();

        Combos_check_analyze_results res = test_results[index];

        taskArray[i] = System.Threading.Tasks.Task.Factory.StartNew((Object obj) =>
        {
            analyze_task(check_nums, ref res);
           },
        i);

}

System.Threading.Tasks.Task.WaitAll(taskArray);



And the analyze_task method now looks like this:

public void analyze_task(int[] check_nums, ref Combos_check_analyze_results result)
{
    combos_check_analyze(check_nums, ref result);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1