Advertisement

06.10.2007 at 03:04PM PDT, ID: 22624978
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Multi-Thread Programming In C# + Thread Management

Asked by secret_wind in C# Programming Language, Windows Programming, Miscellaneous Programming

Tags: ,

Dear Experts,
Well, I've written this piece of Code and I expect it to work like this :

A1 B1 A1
B2 A2 A2
-  B3  -
....
to row99

and like this with mess and without any patterns .
I think it's because of the Variable "Counter" and because of that each (counter)
variable in each delegates of the threads are different but I expect 'em not to be
different .
I don't know but I think the ARRAY are same ! and actually they write in the same array !

Can you please help me to fix this problem ... it's very confusing because I'm not
familiar with threads ...

and to Fix the mess and make the array like this

A1   A1  A1
B2   B2  B2
...
to row99


should I use " LOCK(THIS) {  }" statement as I did to lock the work of each
Thread or something else !?
please guide me as an amature because I'm so confused using threads !

I write you the code below ... thanks for your help !


--------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] One = new string[100, 4];
            int counter = 0;
            for (int i = 0; i < 100; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    One[i, j] = "-";
                }
               
            }
            myProgram myP = new myProgram();
            myP.Start(ref One,ref counter);
           
        }
    }

    public class myProgram
    {
        public void Start(ref string[,] InputArray, ref int RowCounter)
        {
           
            object Arr = InputArray;
            Hashtable Params = new Hashtable();
            Params["Array"] = Arr;
            Params["Counter"] =  RowCounter;
           



            Thread ProcessA = new Thread(new ParameterizedThreadStart(ProcessA_Work));
            Thread ProcessB = new Thread(new ParameterizedThreadStart(ProcessB_Work));
           
            ProcessA.Start(Params);
            ProcessB.Start(Params);        
           

        }

        public void ProcessA_Work(object Input)
        {
            string[,] Array = (string[,])((Hashtable)Input)["Array"];
            int counter = Int32.Parse(((Hashtable)Input)["Counter"].ToString());
            //lock (this)
            //{
                while ( counter < 100)
                 {
                    for (int j = 0; j <= 3; j++)
                    {
                        Thread.Sleep(10);
                        Array[counter, j] = "A" + counter.ToString();
                        Thread.Sleep(200);
                    }

                    counter++;
                }
            //}
           
        }

        public void ProcessB_Work(object Input)
        {
            string[,] Array = (string[,])((Hashtable)Input)["Array"];
            int counter = Int32.Parse(((Hashtable)Input)["Counter"].ToString());
            //lock (this)
            //{
            while (counter < 100)
            {
                for (int j = 0; j <= 3; j++)
                {
                    Thread.Sleep(10);
                    Array[counter, j] = "B" + counter.ToString();
                    Thread.Sleep(10);
                }
                counter++;

            }
            //}
           
        }
    }
}
Start Free Trial
 
Loading Advertisement...
 
[+][-]06.11.2007 at 07:13AM PDT, ID: 19258254

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C# Programming Language, Windows Programming, Miscellaneous Programming
Tags: array, thread
Sign Up Now!
Solution Provided By: DarrenD
Participating Experts: 2
Solution Grade: A
 
 
[+][-]06.11.2007 at 11:56AM PDT, ID: 19260592

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]07.10.2007 at 06:18PM PDT, ID: 19459141

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]07.19.2007 at 03:44PM PDT, ID: 19527019

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32