gleichzeitige asynchrone Operationen mit unterschiedlichen Rückgabetypen
verwalten
public class AsyncOperations { private async Task<Cat> FeedCatAsync() { ... } private async Task<House> SellHouseAsync() { ... } private async Task<Tesla> BuyCarAsync() { ... } }
Task.WhenAll
// Instantiate the class containing asynchronous methods var operations = new AsyncOperations(); // Initiate the tasks var catTask = operations.FeedCatAsync(); var houseTask = operations.SellHouseAsync(); var carTask = operations.BuyCarAsync(); // Execute concurrently and wait for completion await Task.WhenAll(catTask, houseTask, carTask); // Retrieve results var cat = await catTask; var house = await houseTask; var car = await carTask; // Process the results...
Task.WhenAll
await
Task.Result
stellt sicher, dass alle Aufgaben vor dem Fortfahren abgeschlossen sind. Das Schlüsselwort
Das obige ist der detaillierte Inhalt vonWie kann ich mehrere asynchrone Aufgaben mit unterschiedlichen Rückgabetypen effizient ausführen und Ergebnisse abrufen?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!