C#线程UI被阻止| WebRequest.Create的可能原因? [英] C# Thread UI is getting blocked | Possible reason WebRequest.Create?

查看:105
本文介绍了C#线程UI被阻止| WebRequest.Create的可能原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到问题,原因是阻塞了我的UI线程.我知道这是在以下功能中发生的:

I'm currently having the issue, that something is blocking my UI thread. I know it is happening in the following function:

public async Task<string> function(string username, string password, string handle)
{
    try
    {
        string finalStr;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://url.com");
        request.CookieContainer = cookie;
        request.AllowAutoRedirect = true;

        var response = await request.GetResponseAsync();

        string str = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();

        string str2 = this.getToken(str, "_token\" value=\"", "\">", 0);
        string[] textArray1 = new string[] { "postVariables=" + str2 };

        HttpWebRequest httpWebRequest_0 = (HttpWebRequest)WebRequest.Create("https://url.com");
        httpWebRequest_0.CookieContainer = cookie;
        httpWebRequest_0.Method = "POST";
        httpWebRequest_0.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
        httpWebRequest_0.Referer = "https://twitter.com/settings/account";
        httpWebRequest_0.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
        httpWebRequest_0.AllowAutoRedirect = true;
        httpWebRequest_0.ContentType = "application/x-www-form-urlencoded";

        byte[] bytes = Encoding.ASCII.GetBytes(string.Concat(textArray1));
        httpWebRequest_0.ContentLength = bytes.Length;

        Stream requestStream = await httpWebRequest_0.GetRequestStreamAsync();
        await requestStream.WriteAsync(bytes, 0, bytes.Length);

        var response2 = await httpWebRequest_0.GetResponseAsync();

        using (StreamReader reader = new StreamReader(response2.GetResponseStream()))
        {
            finalStr = reader.ReadToEnd();
        }

        if (finalStr.Contains(handle))
        {
            return "success";
        }
        else
        {
            requestStream.Close();
            return "error";
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

我相信这是该功能的一部分:

I believe it's this part of the function:

HttpWebRequest httpWebRequest_0 = (HttpWebRequest)WebRequest.Create("https://url.com");

如何创建async WebRequest.Create?还有我在做错什么吗?

How could I create a async WebRequest.Create? Is there something else I'm doing wrong?

我感谢您提供的任何帮助和建议.

I appreciate any kind of help and suggestions.

推荐答案

由于 Dns.GetHostByName 在内部是一种阻塞方法(有时确实很慢),此时您的代码可以被阻塞.

Since WebRequest.Create uses Dns.GetHostByName internally which is a blocking method (and sometimes really slow) your code can be blocked at that point.

一个简单的解决方法是创建一个任务并取消它

A simple workaround can be creating a task and awating it

HttpWebRequest request = await Task.Run(()=> WebRequest.Create("https://google.com") as HttpWebRequest);

这篇关于C#线程UI被阻止| WebRequest.Create的可能原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆