我试图在一个单独的非 ui 线程上下载一个字符串,当它完成下载更新 ui 时,但我想在没有 ui 冻结的情况下这样做,所以我尝试创建一个线程但没有用,然后我尝试在该线程中创建一个线程,但这也不起作用如何在不冻结 ui 的情况下调用 downloadstring 函数?
public partial class Form1 : Form
{
private delegate void displayDownloadDelegate(string content);
public Thread downloader, web;
public Form1()
{
InitializeComponent();
}
// Go (Download string from URL) button
private void button1_Click(object sender, EventArgs e)
{
textBox1.Enabled = false;
string url = textBox1.Text;
Thread web = new Thread(() => webDownload(url));
web.Start();
}
// Go (sorting) button
private void button2_Click(object sender, EventArgs e)
{
}
public void webDownload(string address)
{
Thread next = new Thread(() => downloading(address));
next.Start();
// next.Join();
}
public void downloading(string address){
using (WebClient client = new WebClient())
{
string content = client.DownloadString(address);
textBox2.BeginInvoke(new displayDownloadDelegate(displayDownload), content);
}
}
private void displayDownload(string content)
{
textBox2.Text = content;
}