操作结束时的BeginInvoke更新控件 [英] BeginInvoke update control at end of operation

查看:108
本文介绍了操作结束时的BeginInvoke更新控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行文件复制程序,其中进度条显示文件复制进度。我已经创建了将执行文件读写工作的线程。

I am doing file copy program with progress bar showing file copied progress.I have created thread which will do the file reading and writing work.

public partial class FileCopy : Form
{
  onStartButtonClick()
  {
      Thread fileReaderWriter = new Thread(new ThreadStart(doFileCopy));
      fileReaderWriter.Start();
  }
  doFileCopy()
  {
    while(file.length> 0)
    {
      //read data 
      //write data
      reportProgress(someValue);
    }
   MessageBox.show("File Copied");
   disableControl();
  }
  private void disableControl()
  {
    progressbar1.hide();
  }
   private void ReportProgress(int _nValue)
        {
            progressBar1.BeginInvoke((Action)(() =>
            {
                progressBar1.Value = _nValue;
                progressBar1.Update();
            }));

            label4.BeginInvoke((Action)(() =>
            {
                label4.Text = " some Text";
            }));

            label5.BeginInvoke((Action)(() =>
            {
                label5.Text = " Time remaining  " + _lTimeRemaining + " s";
            }));       
        }
}
<pre>
Progress bar is get updated its not hiding the progressbar after file copied message.It get hange on disableControl() method.How can I hide progress bar?

推荐答案

为什么在创建的线程上调用加入?该操作导致UI线程被阻塞,直到创建的线程完成其工作...

Why do you call Join on the created thread? That operation causes the UI thread to be blocked until the created thread has finished its work...


这篇关于操作结束时的BeginInvoke更新控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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