-1

Is it possible to do the following using the new Async/Await features in c#:

static void Main(string[] args)
{
    Thread[] thread = new Thread[101];

    for (int i = 0; i < 101; i++)
    {
        thread[i] = new Thread(IntenseWork);
        thread[i].Start();
    }

    Finish(thread);
}

private static void IntenseWork()
{
    Thread.Sleep(5000);
}

private static void Finish(Thread[] threadArray)
{
    foreach (Thread ta in threadArray)
    {
        ta.Join();
    }

    Console.WriteLine("Press Any Key to Continue...");
    Console.ReadKey();
}

I am aware there are limitations of Async/Await that you can only do using thread. But for an example like this where there is one function DoWork that needs to be ran on several threads at the same time, can it be done?

Blake Rivell
  • 13,105
  • 31
  • 115
  • 231
  • 1
    Create a `Task` collection and use `Task.WhenAll` – Nkosi Oct 04 '17 at 14:43
  • @Nkosi can you give me a small sample of how I can do that? – Blake Rivell Oct 04 '17 at 14:45
  • 2
    Someone already provided an answer. you should also take some time and read this Reference [Async/Await - Best Practices in Asynchronous Programming](https://msdn.microsoft.com/en-us/magazine/jj991977.aspx) – Nkosi Oct 04 '17 at 14:50
  • If given duplicate (that ask *exactly* the same question) is not enough consider searching https://www.bing.com/search?q=c%23+convert+thread+to+task+async and clarifying why existing solutions did not work by making [edit] of the question so it can be re-opened. – Alexei Levenkov Oct 04 '17 at 15:06

1 Answers1

-1

this?

    static void Main(string[] args)
    {
        Task[] tasks = new Task[101];

        for (int i = 0; i < tasks.Length; i++)
        {
            tasks[i] = IntenseWork();
        }
        Task.WaitAll(tasks);
        Console.WriteLine("Press Any Key to Continue...");
        Console.ReadKey();
    }

    private static async Task IntenseWork()
    {
        await Task.Delay(5000);
    }
mikelegg
  • 1,197
  • 6
  • 10
  • I see so I am dealing with Tasks instead of Threads and then can call Task.WaitAll instead of doing Thread.Join. That is the only difference? – Blake Rivell Oct 04 '17 at 14:49
  • 1
    @BlakeRivell that "only" is as big as an iceberg. – Panagiotis Kanavos Oct 04 '17 at 14:53
  • @PanagiotisKanavos I got ya, but I meant as far as my example goes. – Blake Rivell Oct 04 '17 at 14:54
  • This isn't a good example. It *doesn't* run anything in the background. `async/await` by itself *doesn't* make a method asynchronous or a background job – Panagiotis Kanavos Oct 04 '17 at 14:55
  • 1
    @BlakeRivell your example is incomplete. .NET has a *lot* of ways to handle concurrent, parallel or asynchronous processing. That's 3 *different* models. Processing 1M data items in parallel isn't the same as handling 100 HTTP requests asynchronously, so that they don't block the calling thread. – Panagiotis Kanavos Oct 04 '17 at 14:56
  • @BlakeRivell Please be aware that this is not the same thing! The original code is creating 101 threads and starting them all. Tasks, on the other end, **are not Threads**, and depending on what `IntenseWork` does this can lead to big differences in behavior and performance. There are alot of resources addressing this, such as [this question](https://stackoverflow.com/questions/4130194/what-is-the-difference-between-task-and-thread) – Tiago Sousa Oct 04 '17 at 14:57