How can I convert this method to async

前端 未结 2 1212
刺人心
刺人心 2021-01-17 06:58

I have a simple method shown below. How can I convert it to async?

public string Smethod0(HttpWebRequest httpWebRequest0)
{
    return new StreamReader(Smet         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-17 07:55

    You can make it async by using async/await mechanism:

    public async Task Smethod0(HttpWebRequest httpWebRequest0)
        {
            return await new StreamReader(Smethod_1(httpWebRequest0).GetResponseStream(),
            Encoding.UTF8, true).ReadToEndAsync();
        }
    

    Also see I'm using ReadToEndAsync() in stead of ReadToEnd().

    And Steve is right, you'll have to dispose the stream first before returning the result.

提交回复
热议问题