0

I'm trying to call the async function getLatestTag in a synchronous one, but I can't get the program to finish for it to execute before continuing.

public static void checkInstalled()
{
    var t = new Task(getLatestTag);
    t.Start();
    if (623 == tagOutput)
    {
        MessageBox.Show("succes");
    }
    else
    {
        MessageBox.Show("fail");
    }
}

private static async void getLatestTag()
{
    var httpClient = new HttpClient();
    var request = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/Futminer/miner-download/releases/latest");
    var productValue = new ProductInfoHeaderValue("FutMiner", "1.0");
    request.Headers.UserAgent.Add(productValue);
    var resp = await httpClient.SendAsync(request);
    HttpContent content = resp.Content;
    string data = await content.ReadAsStringAsync();
    dynamic obj = JsonConvert.DeserializeObject(data);
    tagOutput = Convert.ToInt32(obj["tag_name"]);
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Futinghe
  • 41
  • 1
  • 5
  • 2
    Short answer: Don't call async code from sync code – DavidG May 27 '22 at 12:02
  • 3
    Also: async void is a really bad practice, you should go and learn how async works before continuing. – DavidG May 27 '22 at 12:03
  • Does this answer your question? [How to wait for async method to complete?](https://stackoverflow.com/questions/15149811/how-to-wait-for-async-method-to-complete) – cidermole May 27 '22 at 12:03
  • 1
    Tasks aren't threads. There's no good reason to call `Task.Start` in application code. How is `checkInstalled` called? That method should be asynchronous itself, going all the way to the top-level event handler that started these calls. The event handler should be `async void` but all the other methods should be `async Task`. Calling `.Wait()` will freeze the UI – Panagiotis Kanavos May 27 '22 at 12:18

2 Answers2

2

Following Panagiotis Kanavos's advice:

Tasks aren't threads. There's no good reason to call Task.Start in application code. How is checkInstalled called? That method should be asynchronous itself, going all the way to the top-level event handler that started these calls. The event handler should be async void but all the other methods should be async Task. Calling .Wait() will freeze the UI

After making every necessary function async and using await when needed I managed to get everything working the way I wanted

Futinghe
  • 41
  • 1
  • 5
-2

First off, never return void from an async method - rather return a Task instance.

Then, if you're certain that you want to handle the async method in a sync method, to force it to wait for the result you need .Result on it.

So your code should be like this:

public static void checkInstalled()
{
    getLatestTag().Wait();
    
    if (623 == tagOutput)
    {
        MessageBox.Show("succes");
    }
    else
    {
        MessageBox.Show("fail");
    }
}

private static async Task getLatestTag()
{
    var httpClient = new HttpClient();
    var request = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/Futminer/miner-download/releases/latest");
    var productValue = new ProductInfoHeaderValue("FutMiner", "1.0");
    request.Headers.UserAgent.Add(productValue);
    var resp = await httpClient.SendAsync(request);
    HttpContent content = resp.Content;
    string data = await content.ReadAsStringAsync();
    dynamic obj = JsonConvert.DeserializeObject(data);
    tagOutput = Convert.ToInt32(obj["tag_name"]);
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
M. Radević
  • 102
  • 4
  • 'Task' does not contain a definition for 'Result' and no accessible extension method 'Result' accepting a first argument of type 'Task' could be found (are you missing a using directive or an assembly reference?) – Futinghe May 27 '22 at 12:11
  • Sorry, I'm used to Task. See my edit. – M. Radević May 27 '22 at 12:14
  • After running the app it doesn't seem to go past "getLatestTag().Wait();" as it doesn't run the following code – Futinghe May 27 '22 at 12:17
  • 1
    `Task.Wait` is almost always a bad idea and can lead to deadlocks. – DavidG May 27 '22 at 12:26