简体   繁体   English

C#线程第二次未更改文本框值

[英]C# Thread not changing the text box values the second time

I am creating an application that involves using threads. 我正在创建一个涉及使用线程的应用程序。 Everything works until I click the button for the second time. 一切正常,直到我第二次单击该按钮。 Nothing happens on the second time the button is clicked. 第二次单击该按钮没有任何反应。 Its like the first time all the stuff loads and then just locks the values of the text boxes. 就像第一次填充所有内容,然后仅锁定文本框的值一样。 The stuff in red is just private links that cannot be shown. 红色的内容只是无法显示的私人链接。 Its not the links because they work just fine the first time. 它不是链接,因为它们在第一次就可以正常工作。 They just won't work the second time. 他们只是第二次不工作。 I hope what I just said wasn't too confusing. 我希望我刚才所说的不会太混乱。

我的代码的图像

name1 , name2 , name3 are all downloaded when the form is created , they're just bound to the textboxes when you press the button the first time. name1name2name3创建窗体时的所有下载的,他们只是必然要当你按下按钮在第一时间文本框。

_name1() , _name2() , _name3() methods are just object instantiations and have no side effects of any kind (put differently, they don't do anything). _name1()_name2()_name3()方法只是对象实例化,没有任何副作用_name2() _name3() ,它们什么也不做)。

And all the threading stuff is just fluff - you're calling methods that don't do anything and then aborting the threads (thereby aborting something that isn't doing anything anyway). 而且所有线程处理工作都只是起毛-您正在调用不执行任何操作的方法,然后中止线程(因此中止了始终不执行任何操作的操作)。 This has zero effect on the execution in any way as the code is currently written, even when executed the first time. 由于当前正在编写代码,因此即使在第一次执行时,这对执行也没有任何影响。

The simple, synchronous fix for your code will look like this: 代码的简单同步修复将如下所示:

private void Button_Click(object sender, EventArgs e)
{
    using (WebClient client = new WebClient())
    {
        textBox1.Text = client.DownloadString("<your URL here>");
        textBox2.Text = client.DownloadString("<your URL here>");
        textBox3.Text = client.DownloadString("<your URL here>");
    }
}

Seeing as you're using threads, your goal is obviously non-blocking, asynchronous execution. 看到您正在使用线程时,您的目标显然是无阻塞的异步执行。 The easiest way to achieve it while preserving the sequencing of operations is with async/await : 保留操作顺序的同时,最简单的方法是使用async/await

private async void Button_Click(object sender, EventArgs e)
{
    // Disabling the button ensures that it's not pressed
    // again while the first request is still in flight.
    materialRaisedButton1.Enabled = false;

    try
    {
        using (WebClient client = new WebClient())
        {
            // Execute async downloads in parallel:
            Task<string>[] parallelDownloads = new[] {
                client.DownloadStringTaskAsync("<your URL here>"),
                client.DownloadStringTaskAsync("<your URL here>"),
                client.DownloadStringTaskAsync("<your URL here>")
            };

            // Collect results.
            string[] results = await Task.WhenAll(parallelDownloads);

            // Update all textboxes at the same time.
            textBox1.Text = results[0];
            textBox2.Text = results[1];
            textBox3.Text = results[2];
        }
    }
    finally
    {
        materialRaisedButton1.Enabled = true;
    }
}

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

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