I searched the site for this question but I could not find a solution to my problem.
The code below works without error, but does not work in the function.
When the Program is run, the pairsprocess function must be called with the given parameters and the string and (i + 6) value given as parameters should be written to the screen.
But despite the code work, it doesn't do what I write.
I tried it in Task. Factory. StartNew and it didn't work that method.
static void Main(string[] args)
{
string[] Pairs = new string[] { "EURUSD", "GBPUSD", "EURGBP" };
int totalPairs = Pairs.Count();
Task[] Proc = new Task[totalPairs];
Console.WriteLine($"Pairs Count : {Pairs.Count()}");
for (int i = 0; i < totalPairs; i++)
{
Proc[i] = Task.Run(() => pairsProcess(Pairs[i],6+i));
}
}
public static void pairsProcess(string a,int Counter)
{
for (int i = 0; i < Counter; i++)
{
Console.WriteLine($"Pairs : {a} Counter : {i+1} / {Counter}");
}
}
This is what I normally want.
I add 6 to the value I value.
EURUSD 0 + 6 = 6 times
Pairs: EURUSD Counter: 1/6
Pairs: EURUSD Counter: 2/6
Pairs: EURUSD Counter: 3/6
Pairs: EURUSD Counter: 4/6
Pairs: EURUSD Counter: 5/6
Pairs: EURUSD Counter: 6/6
GBPUSD 1 + 6 = 7 times
Pairs: GBPUSD Counter: 1/7
Pairs: GBPUSD Counter: 2/7
Pairs: GBPUSD Counter: 3/7
Pairs: GBPUSD Counter: 4/7
Pairs: GBPUSD Counter: 5/7
Pairs: GBPUSD Counter: 6/7
Pairs: GBPUSD Counter: 7/7
EURGBP 2 + 6 = 8 times
Pairs: EURGBP Counter: 1/8
Pairs: EURGBP Counter: 2/8
Pairs: EURGBP Counter: 3/8
Pairs: EURGBP Counter: 4/8
Pairs: EURGBP Counter: 5/8
Pairs: EURGBP Counter: 6/8
Pairs: EURGBP Counter: 7/8
Pairs: EURGBP Counter: 8/8
I found the solution to my problem. After running the TASK, the thread had to wait for a little while. My problem is completely solved.
Proc [i] = Task. Run (() = > A. pairsprocess (Pairs [i], 6 + i));
System. Threading. Thread. Sleep (100);