在另一个类中增加ProgressBar

时间:2015-04-01 08:35:07

标签: c# multithreading winforms backgroundworker

这是我在这里问的第一个问题,所以请温柔地对待我;)

所以我实际上在我正在写的C#应用​​程序中有两个WinForms(我对C#很新)。

此窗口有一个按钮,用于将您之前在列表框中选择的USB设备中的照片保存到另一个文件夹。 单击此按钮后,我的主线程当然忙于复制,所以我决定创建另一个包含ProgressBar的WinForm。

Foreach完成副本,我想相应地增加我的ProgressBar。

所以我计算了我必须做的副本数量,并将进度条作为最大值。但我现在的问题是,我真的不知道如何在不得到线程不安全异常的情况下增加ProgressBar。

这是我的ProgressWindow代码:

public partial class ProgressWindow : Form
{
    BackgroundWorker updateProgressBarThread = new BackgroundWorker();

    private Boolean _isThreadRunning = false;
    public Boolean IsThreadRunning
    {
        get { return _isThreadRunning; }
        set { _isThreadRunning = value; }
    }

    private int _progressbarLength;
    public int ProgressbarLength
    {
        get { return _progressbarLength; }
        set { _progressbarLength = value; }
    }

    private int progress = 1;

    public ProgressWindow()
    {
        Show();
        InitializeComponent();
    }

    private void StartUpdateThread(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        // Reports progress to the ProgressChangedEvent function. (Thread Safe)

    }

    private void FinishProgressThread(object sender, RunWorkerCompletedEventArgs e)
    {
        if (!_isThreadRunning)
        {
            MessageBox.Show("Erfolgreich kopiert");
            Close();
        }

    }

    private void ProgressChangedEvent(object sender, ProgressChangedEventArgs e)
    {
        this.copyProgressbar.Value = e.ProgressPercentage;
        this.progressStatus.Text = e.ProgressPercentage.ToString();
    }

    public void CallUpdateThread()
    {
        updateProgressBarThread.WorkerReportsProgress = true;

        updateProgressBarThread.DoWork += new DoWorkEventHandler(StartUpdateThread);
        updateProgressBarThread.ProgressChanged += new ProgressChangedEventHandler(ProgressChangedEvent);
        updateProgressBarThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(FinishProgressThread);
        updateProgressBarThread.RunWorkerAsync();
    }

}

我想在每次成功复制后用1增加ProgressBar。 我如何从我的主线程中执行此操作?

这是实际处理复制过程的功能

private void SaveFile(System.IO.DirectoryInfo root)
{
    try
    {
        IEnumerable<DirectoryInfo> directoriesNames = root.EnumerateDirectories();

        // New instance of thread ProgressWindow.
        ProgressWindow progress = new ProgressWindow();
        progress.CallUpdateThread();

        foreach (DirectoryInfo element in directoriesNames)
        {
            // Query all subdirectories and count everything with the in the configuration made settings.
            if (!element.Attributes.ToString().Contains("System"))
            {
                // Now we insert the configuration we applied.
                String fileExtension = null;

                if (Properties.Settings.Default._configPhoto)
                {
                    fileExtension = "*.jpg";
                }

                if (Properties.Settings.Default._configWordDocument)
                {
                    fileExtension = "*.odt";
                }

                FileInfo[] jpgList = element.GetFiles(fileExtension, SearchOption.AllDirectories);

                // set the size of the progress bar
                progress.ProgressbarLength = jpgList.Count();

                // Now we go through all our results and save them to our backup folder.
                foreach (FileInfo tmp in jpgList)
                {
                    string sourceFilePath = tmp.FullName;
                    string destFilePath = PATHTOBACKUP + "\\" + tmp.Name;
                    progress.IsThreadRunning = true;

                    try
                    {                                
                        System.IO.File.Copy(sourceFilePath, destFilePath, true);
                    }
                    catch (IOException ioe)
                    {
                        MessageBox.Show(ioe.Message);
                    }
                }
            }
        }
        // progress.IsThreadRunning = false;
    }
    catch (UnauthorizedAccessException e)
    {
        MessageBox.Show(e.Message);
    }
}

很明显,我必须在此功能之后执行此操作

System.IO.File.Copy(sourceFilePath, destFilePath, true);

但是如何向ProgressWindow报告此内容?

我真的希望我能够很好地解释它,不确定我是否遗漏了一些重要内容。

先谢谢你们

1 个答案:

答案 0 :(得分:1)

以下是关键组件的简洁示例:

  • 单击按钮启动新线程工作程序
  • 进度由文件长度完成,而不是由文件数
  • 完成
  • BeginInvoke用于更新进度条(避免交叉线程异常)

        ProgressBar pb = new ProgressBar() { Minimum = 0, Maximum = 100 };
        Button btn = new Button();
        btn.Click += delegate {
            Thread t = new Thread(() => {
                DirectoryInfo dir = new DirectoryInfo("C:\\temp\\");
                var files = dir.GetFiles("*.txt");
                long totalLength = files.Sum(f => f.Length);
                long length = 0;
                foreach (var f in files) {
                    length += f.Length;
                    int percent = (int) Math.Round(100.0 * length / totalLength);
                    pb.BeginInvoke((Action) delegate {
                        pb.Value = percent;
                    });
    
                    File.Copy(f.FullName, "...");
                }
            });
            t.IsBackground = true;
            t.Start();
        };
    
相关问题