简体   繁体   English

异步任务<string>不返回正确的结果,WebClient.OpenReadTaskAsync()</string>

[英]async Task<String> not return correct result,WebClient.OpenReadTaskAsync()

in winform program(C#),async Task not return correct result,my codes are below:在 winform 程序(C#)中,异步任务没有返回正确的结果,我的代码如下:

public async void method1()
{
    await Task.Run(() =>
        {
            System.Threading.Thread.Sleep(2000);
            if (textBox1.InvokeRequired)
            {
                textBox1.Invoke(new Action(() => textBox1.Text += "AAAAAAAAAAAA\r\n"));
            }
        });

    if(textBox1.InvokeRequired)
    {
        textBox1.Invoke(new Action(() => textBox1.Text += "BBBBBBBBBB\r\n"));
    }
}

public async Task<string> method2()
{
    string result = string.Empty;
    System.Net.WebClient wc = new System.Net.WebClient();
    Stream st = await wc.OpenReadTaskAsync("https://msdn.microsoft.com/en-us/library/hh191443(v=vs.110).aspx#What Happens in an Async Method");
    //Task<Stream> st= wc.OpenReadTaskAsync("https://msdn.microsoft.com/en-us/library/hh191443(v=vs.110).aspx#What Happens in an Async Method");
    //Stream st2 = st.Result;
    StreamReader sr = new StreamReader(st);
    result = sr.ReadToEnd();

    return result;
}
private void button1_Click(object sender, EventArgs e)
{
    textBox1.Text += method2()+"\r\n";

    method1();
    textBox1.Text += "CCCCCCCCCCCC\r\n";
}

Output: Output:

System.Threading.Tasks.Task`1[System.String] System.Threading.Tasks.Task`1[System.String]

CCCCCCCCCCCC中交CCCCCCCC

AAAAAAAAAAAA啊啊啊啊啊

My question:我的问题:

1)method2,i want to return the page content,but in fact,it is "System.Threading.Tasks.Task`1[System.String]" 1)方法2,我想返回页面内容,其实是“System.Threading.Tasks.Task`1[System.String]”

2)string "BBBBBBBBBB" not been added in textBox1,is this why? 2)textBox1中没有添加字符串“BBBBBBBBBB”,这是为什么?

Here you have the code with some changes (textBox for richTextBox) and some clarifications.这里有一些更改的代码(richTextBox 的文本框)和一些说明。

string "BBBBBBBBBB" not been added in textBox1,is this why? textBox1中没有添加字符串“BBBBBBBBBB”,这是为什么呢?

InvokeRequired only return true if you call it from a worker thread. InvokeRequired 仅在您从工作线程调用时才返回 true。

method2,i want to return the page content,but in fact,it is "System.Threading.Tasks.Task`1[System.String]"方法2,我想返回页面内容,其实是“System.Threading.Tasks.Task`1[System.String]”

Since method1 return Task you have few options, call method.result (not recommende, it will block the main thread) or use async/await to get the result.由于 method1 return Task 你没有什么选择,调用 method.result (不推荐,它会阻塞主线程)或使用 async/await 来获取结果。

    public async void method1()
    {
        await Task.Run(() =>
        {
            System.Threading.Thread.Sleep(2000);
            if (richTextBox1.InvokeRequired)
            {
                richTextBox1.Invoke(new Action(() => richTextBox1.Text += "AAAAAAAAAAAA\r\n"));
            }
        });

        //This in main thread so InvokeRequired == false
        //if (textBox1.InvokeRequired)
        //{
        //    textBox1.Invoke(new Action(() => textBox1.Text += "BBBBBBBBBB\r\n"));
        //}            
        richTextBox1.Text += "BBBBBBBBBB\r\n";
    }

    public async Task<string> method2()
    {
        string result = string.Empty;
        System.Net.WebClient wc = new System.Net.WebClient();
        var st = await wc.OpenReadTaskAsync("https://msdn.microsoft.com/en-us/library/hh191443(v=vs.110).aspx#What Happens in an Async Method");
        //Task<Stream> st= wc.OpenReadTaskAsync("https://msdn.microsoft.com/en-us/library/hh191443(v=vs.110).aspx#What Happens in an Async Method");
        //Stream st2 = st.Result;
        StreamReader sr = new StreamReader(st);
        result = sr.ReadToEnd();

        return result;
    }
    private async void button1_Click(object sender, EventArgs e)
    {
        //await is needed to get string insted of Task<string>
        richTextBox1.Text = await method2(); 

        method1();
        richTextBox1.Text += "CCCCCCCCCCCC\r\n";
    }

Change your button click event like this像这样更改您的按钮单击事件

private async void Button_Click..

Replace代替

textBox1.Text += method2()+"\r\n";

To

textBox1.Text += await method2()+"\r\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM