Avatar ·

How to call an async method from a synchronous method in C#? Problem with deadlock and await

Hello everyone! I'm new to C# and trying to understand async/await. I have a WinForms application, and I'm using a library that only provides async methods (e.g., `HttpClient.SendAsync()`). The problem is that I need to call this async method from a synchronous method, which is called from a button click event. I can't make the entire method async because it would break the logic of other parts of the program (e.g., calling from a constructor or a property). Here's a simplified example of my code: ```csharp public string GetData() { // This is a synchronous method that should return a result var client = new HttpClient(); var task = client.GetStringAsync("https://example.com"); task.Wait(); // Trying to wait for the result retu