Title here
Summary here
This example demonstrates how to use tasks for synchronization in C#. We’ll create a method that simulates work and use a TaskCompletionSource to signal when the work is done.
To run the program, compile and execute it:
If you removed the await tcs.Task;
line from this program, the program would exit before the Worker
even started.
In this C# version:
Task
instead of a goroutine to represent concurrent work.TaskCompletionSource<bool>
to signal completion.Worker
method is marked as async
and uses await Task.Delay(1000)
to simulate work.Main
method, we start the worker task and then wait for it to complete using await tcs.Task
.This approach achieves similar synchronization to the original example, ensuring that the main program doesn’t exit until the worker task is complete.