Home > database >  When I using a thread to run HttpClient.GetAsync(...) and it never returns response
When I using a thread to run HttpClient.GetAsync(...) and it never returns response

Time:08-17

I using a C# Console app to test Httpclient GetSync().

When code run in getsync() and just close done my app.I need to get the response result. Is the thread and task await to conflict or the thread is not the main thread?

Where the code i misusing.

Program.cs

using System;
using System.Threading;
using AutoProcessWorks;

_AutoProcessWorks autoProcessWorks = new _AutoProcessWorks();
Thread AutoRunThread = new Thread(new ThreadStart(autoProcessWorks.MainRun));
AutoRunThread.Start();

AutoProcessWorks.cs is here

public class _AutoProcessWorks
{
    Api api = new Api();
    SqlTools sqlTools = new SqlTools();
    public bool Switch_Run = true;
    public void MainRun()
    {
        int sleep = 60000;
        Api api = new Api();
        while (Switch_Run)
        {
            var route = "IsNFTContractEnough";
            var res = api.CallPythonApiGet(route);
            Console.WriteLine(res);
            Switch_Run = false;
        }
    }
public class Api
{
    public async Task<string> CallPythonApiGet(string route)
    {
        const string uri = "http://127.0.0.1:5000/";
        using (HttpClient client = new HttpClient())
        {
            var respones_result = "";
            try
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.Timeout = TimeSpan.FromSeconds(600);
                var response = await client.GetAsync(uri   route);
                respones_result = response.Content.ReadAsStringAsync().Result;
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                return responseBody;
            }
            catch (HttpRequestException e)
            {
                var error = "";
                error  = respones_result   "\n\r";
                error  = "\nException Caught! Message :"   e.Message;
                return error;
            }
        }
    }
}

CodePudding user response:

You are missing await all over the place, and you should not start a new thread, just call await MainRun()

_AutoProcessWorks autoProcessWorks = new _AutoProcessWorks();
await autoProcessWorks.MainRun();
public class _AutoProcessWorks
{
    Api api = new Api();
    SqlTools sqlTools = new SqlTools();
    public bool Switch_Run = true;

    public async Task MainRun()
    {
        int sleep = 60000;
        Api api = new Api();
        while (Switch_Run)
        {
            var route = "IsNFTContractEnough";
            var res = await api.CallPythonApiGet(route);
            Console.WriteLine(res);
            Switch_Run = false;
        }
    }
}
public class Api
{
    static HttpClient client = new HttpClient(); // keep only one of these around

    public async Task<string> CallPythonApiGet(string route)
    {
        const string uri = "http://127.0.0.1:5000/";
        var respones_result = "";
        try
        {
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.Timeout = TimeSpan.FromSeconds(600);
            using var response = await client.GetAsync(uri   route);
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            return responseBody;
        }
        catch (HttpRequestException e)
        {
            var error = respones_result   "\n\r"   "\nException Caught! Message :"   e.Message;
            return error;
        }
    }
}
  • Related