none
Deleted RRS feed

All replies

  • Since tasks are not executed immediately, i is not incremented, therefore your for loop creates a large amount of tasks, greater than b. Therefore, some of the tasks will see an invalid i.

    In addition, ‘i++’ should be replaced with ‘Interlocked.Increment(ref i)’.

    The implementation has to be revised.

    Check this too: ‘int b = list.Length’.


    • Edited by Viorel_MVP Wednesday, March 20, 2019 9:18 PM
    Wednesday, March 20, 2019 9:17 PM

  • I constantly get this error(Index was outside of the bounds of the array) help

    Check something: 

    See if you have a '\n' (newline - CRLF) at the end of the last line in the file.

    If not, then add one by pressing enter in an editor at the end of that last line.

    Then see if you still get the index out of bounds error.

    - Wayne

    Wednesday, March 20, 2019 9:18 PM
  • Your usage of the for loop is wrong, becuase there is nothing to stop the for loop execuition based on index count going > than the length of the array.

    https://www.dotnetperls.com/for

    for (int i = 0; i < list.length; i++)

    {

         i++;  //It does not belong in the execution path in the for loop. Loop logic internally doesn't recognize it and the stop sign is being ran. 

    {

    |I think it should look like the above.



    • Edited by DA924x Wednesday, March 20, 2019 9:31 PM
    Wednesday, March 20, 2019 9:30 PM

  •             for (int i = 0; i < b;)
                {
                    Task.Factory.StartNew(() =>
                   {
                       Console.WriteLine(list[i]);
                       i++;
                   });
                }
    
    

    I constantly get this error(Index was outside of the bounds of the array) help

    It's apparent that the issue is arising because of your use of 
    Task.Factory.StartNew. Why are you doing that?

    - Wayne

    Wednesday, March 20, 2019 9:33 PM
  • Deleted
    Wednesday, March 20, 2019 9:36 PM
  • Well I'm going to make a program that does some caluclations that last several hours.And so I think it would be most convinient to put each input fot those calculations in its own thread so it can run more of these calcualtions at a time

    Consider the following pattern

    https://stackoverflow.com/questions/13695499/proper-way-to-implement-a-never-ending-task-timers-vs-task


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, March 20, 2019 9:47 PM
    Moderator
  •             for (int i = 0; i < b;)
                {
                    Task.Factory.StartNew(() =>
                   {
                       Console.WriteLine(list[i]);
                       i++;
                   });
                }
    

    I constantly get this error(Index was outside of the bounds of the array)

    Appears to be a race condition. If you step through your code, pausing very 
    briefly after each WriteLine, you will not get the index error. But if you 
    let it run at full throttle you will get the exception.

    You can also see the effect if you introduce a short pause on each iteration.
    e.g. -  Thread.Sleep(250)

    Read:

    Deadlocks and race conditions
    https://docs.microsoft.com/en-us/dotnet/standard/threading/managed-threading-best-practices#deadlocks-and-race-conditions

    - Wayne

    Wednesday, March 20, 2019 10:03 PM
  • In order to process the lines in parallel, consider this approach too:

       Parallel.ForEach( list, line =>

          {

             Console.WriteLine( line );

          } );

     

    It depends on kind of your data and calculations.

    Wednesday, March 20, 2019 10:05 PM
  • Hi BataBo Jokviu,

    Your lambda expression (() =>
                   {
                       Console.WriteLine(list[i]);
                       i++;
                   }) captures the i variable by reference.
    Therefore, when the lambda expression runs after i++, it picks up the new value of i.

    I would suggest that you could declare a separate variable inside the loop so that each lambda gets its own variable. 

    for (int i = 0; i < b; )
                {
                    int localnum = i;
                    Task.Factory.StartNew(() =>
                    {
                        Console.WriteLine(list[localnum]);
                    });
                    i++;
                }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 21, 2019 7:11 AM
    Moderator