0

我有一种情况,我只需要一个标签,当用户单击按钮时,该标签在“就绪”和“进行中”之间切换。标签最初处于“就绪”状态。当用户单击按钮时,标签应显示“进行中”,然后需要执行一些任务,例如复制文件等。任务成功完成后,标签应再次显示“就绪”。现在我正在使用这段代码,标签状态没有改变。我怎样才能使这项工作。请帮忙。

 private void button1_Click(object sender, EventArgs e)
 {
        status.Text = "In Progress";
        if (listBox1.Items.Count == 0)
        {
            MessageBox.Show("Please select a file to upload");
        }
        FtpClient client = new FtpClient("*******", "*******", "******");
        string fileName = getFileNameFromPath(listBox1.Items[0].ToString());
        string localFile = listBox1.Items[0].ToString();
        string remoteFile = "**********/"+fileName;
        string link = client.upload(remoteFile, localFile);
        listBox1.Items.RemoveAt(0);
        textBox1.Text = link; 
        status.Text = "Ready";  
  }
4

1 回答 1

2

您在长时间运行的过程中阻塞了 UI 线程,既阻止 UI 更新文本值,也阻止接收用户输入,或者为此做任何事情。

您需要异步执行长时间运行的工作,以免阻塞 UI 线程。

理想情况下,您将拥有一个由您提供的异步方法FtpClient(甚至更好,它会返回 a Task)。这将允许您编写如下内容:

private async void button1_Click(object sender, EventArgs e)
{
    status.Text = "In Progress";
    if (listBox1.Items.Count == 0)
    {
        MessageBox.Show("Please select a file to upload");
    }
    FtpClient client = new FtpClient("*******", "*******", "******");
    string fileName = getFileNameFromPath(listBox1.Items[0].ToString());
    string localFile = listBox1.Items[0].ToString();
    string remoteFile = "**********/" + fileName;
    string link = await client.uploadAsync(remoteFile, localFile);
    listBox1.Items.RemoveAt(0);
    textBox1.Text = link;
    status.Text = "Ready";
}

然后你就完成了。如果它不提供任何异步方法,那么作为一种解决方法,您可以启动一个新任务来在后台完成工作:

private async void button1_Click(object sender, EventArgs e)
{
    status.Text = "In Progress";
    if (listBox1.Items.Count == 0)
    {
        MessageBox.Show("Please select a file to upload");
    }
    FtpClient client = new FtpClient("*******", "*******", "******");
    string fileName = getFileNameFromPath(listBox1.Items[0].ToString());
    string localFile = listBox1.Items[0].ToString();
    string remoteFile = "**********/" + fileName;
    string link = await Task.Run(() => client.upload(remoteFile, localFile));
    listBox1.Items.RemoveAt(0);
    textBox1.Text = link;
    status.Text = "Ready";
}

如果您没有 C# 5.0 和 .NET 4.5 可以使用,await那么您可以使用BackgroundWorker

private void button1_Click(object sender, EventArgs e)
{
    status.Text = "In Progress";
    if (listBox1.Items.Count == 0)
    {
        MessageBox.Show("Please select a file to upload");
    }
    string fileName = getFileNameFromPath(listBox1.Items[0].ToString());
    string localFile = listBox1.Items[0].ToString();
    string remoteFile = "**********/" + fileName;
    var worker = new BackgroundWorker();
    worker.DoWork += (s, args) =>
    {
        FtpClient client = new FtpClient("*******", "*******", "******");
        args.Result =  client.upload(remoteFile, localFile);
    };
    worker.RunWorkerCompleted += (s, args) =>
    {
        listBox1.Items.RemoveAt(0);
        textBox1.Text = args.Result as string;
        status.Text = "Ready";
    };
    worker.RunWorkerAsync();
}
于 2013-10-10T20:11:00.593 回答