Skip to content

Task Status #103428

Open
Open

Description

Description

In the tasks, for some reason, the status shows that they have already been completed, but they are still running. Because of this, Task.WaitAll does not stop them, as it thinks they have already finished. This is a strange behavior.
image

Reproduction Steps

using System;
using System.Collections.Concurrent;

var response = Enumerable.Range(0, 1000);

var res = response.Chunk(100);


var ls = new List<Task>();
CancellationTokenSource factory = new();
ConcurrentBag<string> info = new ConcurrentBag<string>();
foreach (var element in res)
{

    var task = new Task(async () =>
    {
        await Task.Delay(1000000, factory.Token)
                        .ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
        foreach (var item in element)
        {
            info.Add(item.ToString());

            Console.Write(item + "|");
        }
    }, TaskCreationOptions.LongRunning);
    ls.Add(task);
    task.Start();
}
await Task.Delay(3000);
factory.Cancel();
Task.WaitAll(ls.ToArray());
Console.WriteLine($"Counts Distinct Items:{info.Distinct().Count()} Count {info.Count}");
Console.ReadKey();

Expected behavior

Actual behavior

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    area-System.Threading.TasksquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions