Last active
November 6, 2023 16:07
-
-
Save rahulsahay19/2ca75cb326cde92438d8672baa8ad8ae to your computer and use it in GitHub Desktop.
TaskContinuationExample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
public class TaskContinuationExample | |
{ | |
public static void Main(string[] args) | |
{ | |
Task<int> originalTask = Task.Run(() => | |
{ | |
// Simulate some time-consuming computation | |
return 27; | |
}); | |
Task<string> continuationTask = originalTask.ContinueWith(previousTask => | |
{ | |
int result = previousTask.Result; | |
return $"The answer is {result}."; | |
}); | |
continuationTask.Wait(); // Wait for the continuation task to complete | |
Console.WriteLine(continuationTask.Result); // Output: "The answer is 27." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment