0

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);
Umit Terzi
  • 106
  • 1
  • 10
  • Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details. Please [edit] these details in or we may not be able to help. – Blue Dec 08 '18 at 01:20
  • I added the problem to my question. I'd appreciate it if you could. – Umit Terzi Dec 08 '18 at 01:25
  • Please at least add some sample *output* from the function. It's clear enough what it is intended to do but what it is doing is not. You seem to be saying it works and that it doesn't work. – ChiefTwoPencils Dec 08 '18 at 01:26
  • Have you tried waiting until all the tasks completed? `Task.WaitAll(Proc)`? – Blue Dec 08 '18 at 01:32
  • I've updated the question and wrote in detail what I want to do. – Umit Terzi Dec 08 '18 at 01:50
  • 1
    It is still very confusing what you actually get as result... This is likely dup of https://stackoverflow.com/questions/271440/captured-variable-in-a-loop-in-c-sharp , but without actual results vs. expected results it is hard to confirm – Alexei Levenkov Dec 08 '18 at 01:53
  • @AlexeiLevenkov I've reviewed the link, that's not the answer to my question. I updated my question and wrote the result I expected. – Umit Terzi Dec 08 '18 at 01:56
  • The fact code is throwing "Index was outside the bounds of the array." makes it exactly the same issue covered in question I linked to (and chose as duplicate) - captured value of `i` is outside of array bounds by the time code actually run. You can find copy-paste ready answer by Rufus L... Side note: for future question please provide exact information how code behaves/fails instead of "not working"... usually questions without such information are just downvoted – Alexei Levenkov Dec 08 '18 at 02:07
  • I'm having an unrelated problem with the question you call Duplicate. I want the words I wrote into a string array and the for loop to give the I value as a parameter to the Pairsprocess function and run them as a parallel task in the For loop. – Umit Terzi Dec 08 '18 at 02:10
  • @UmitTerzi you should ask another question as this one clearly has issue with capturing `i` value in the loop and also somehow you could not provide actual output. Maybe when you fix problem pointed out by Rufus L (and explained in the duplicate) you'll be able to capture output so new question can clearly show expected vs. actual results. – Alexei Levenkov Dec 08 '18 at 02:30
  • @UmitTerzi on other hand maybe you can be happy with IndexOutOfBounds exception as current code does exactly what you want - pass current value of `i` to `pairsProcess` method... also indeed that value in most cases would be 4 as tasks will usually start after loop is over and i=4... – Alexei Levenkov Dec 08 '18 at 02:34
  • 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. I did not write this solution as an answer to the bottom of my question Proc [i] = Task. Run (() = > A. pairsprocess (Pairs [i], 6 + i)); System. Threading. Thread. Sleep (100); – Umit Terzi Dec 08 '18 at 02:45

1 Answers1

2

I think the problem is that you need to capture the value of I inside the loop instead of using it directly in the task.

Try something like this instead:

for (int i = 0; i < totalPairs; i++)
{
    var value = i;
    Proc[i] = Task.Run(() => pairsProcess(Pairs[value], 6 + value));
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • OP explicitly said this is not the case - as they declined the fact it is covered in https://stackoverflow.com/questions/271440/captured-variable-in-a-loop-in-c-sharp (also indeed it is the problem they just disagree that "Index was outside the bounds of the array." caused by accessing elements by `totalPairs` index). Closed as duplicate. – Alexei Levenkov Dec 08 '18 at 01:58
  • @Rufus L Every time I run it, it produces different results. The writing in the loop is either incomplete or does not write at all. – Umit Terzi Dec 08 '18 at 02:03
  • @Nkosi I don't fully understand it, but I believe it has to do with scoping of the variable inside the task - somehow the value isn't captured until the task runs, and because the loop is so short it completes before the tasks begin execution, so they all have the same (last) value of `i`. – Rufus L Dec 08 '18 at 02:03
  • @UmitTerzi The order of the results may vary, but for me I get the same number of results every time (using your exact code with my change). But this does seem like a duplicate of the other question now that I look at that – Rufus L Dec 08 '18 at 02:05
  • I'm having an unrelated problem with the question you call Duplicate. I want the words I wrote into a string array and the for loop to give the I value as a parameter to the Pairsprocess function and run them as a parallel task in the For loop. – Umit Terzi Dec 08 '18 at 02:09
  • 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. I did not write this solution as an answer to the bottom of my question Proc [i] = Task. Run (() = > A. pairsprocess (Pairs [i], 6 + i)); System. Threading. Thread. Sleep (100); – Umit Terzi Dec 08 '18 at 02:44