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(P
rocessA_Wo
rk));
Thread ProcessB = new Thread(new ParameterizedThreadStart(P
rocessB_Wo
rk));
ProcessA.Start(Params);
ProcessB.Start(Params);
}
public void ProcessA_Work(object Input)
{
string[,] Array = (string[,])((Hashtable)Inp
ut)["Array
"];
int counter = Int32.Parse(((Hashtable)In
put)["Coun
ter"].ToSt
ring());
//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)Inp
ut)["Array
"];
int counter = Int32.Parse(((Hashtable)In
put)["Coun
ter"].ToSt
ring());
//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