为什么WinForm被BackgroundWorker卡住了? [英] Why WinForm Gets Stuck with BackgroundWorker?

查看:70
本文介绍了为什么WinForm被BackgroundWorker卡住了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用此代码复制一些文件,一切都很好,该应用程序将复制文件,但是在复制过程中,我无法移动我的应用程序或执行任何操作我试图使用线程,但是它不起作用,我也使用backgroundWorker,但是仍然没有什么不被卡住的唯一控件是progressBar,它的工作原理很好,是我的代码:

hi guys i tried to copy some files with this Code everything is good and the app will copy files but in copy progress i cant move my app or do anything i tried to use thread but its not works i also use backgroundWorker but still nothing the only control that doesnt get stuck is progressBar its works fine here is my code :

 public Form1()
    {
        InitializeComponent();
        backgroundWorker1.Dispose();
        backgroundWorker1.DoWork += BackgroundWorker_DoWork;
        backgroundWorker1.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
        backgroundWorker1.ProgressChanged += BackgroundWorker_ProgressChanged;
        backgroundWorker1.WorkerReportsProgress = true;

        backgroundWorker2.DoWork += BackgroundWorker2_DoWork;
        backgroundWorker2.WorkerReportsProgress = true;
    }

    private void BackgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
    {
        File.Copy(sourcePath, targetPath);
    }

    private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < fileSize; i++)
        {
            int p = (i + 1) * 100 / Convert.ToInt32(fileSize);
            backgroundWorker1.ReportProgress(p);
        }
    }

    private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

    }

    private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        lbProgress.Text = e.ProgressPercentage.ToString();
        progressBar1.Value = e.ProgressPercentage;
    }

    private void btnTarget_Click(object sender, EventArgs e)
    {
        folderBrowser = new FolderBrowserDialog();
        if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            targetPath += folderBrowser.SelectedPath + @"\" + fileName;
            lbTarget.Text = targetPath;
        }
    }

    private void btnSource_Click(object sender, EventArgs e)
    {
        op = new OpenFileDialog();
        if (op.ShowDialog() == DialogResult.OK)
        {
            sourcePath += op.FileName;
            lbSource.Text = sourcePath;
            fileInfo = new FileInfo(sourcePath);
            fileSize = fileInfo.Length / 1024;
            fileName = fileInfo.Name;
            MessageBox.Show(string.Format("File size is: {0} KB", fileSize));
        }
    }

    private void btnCopy_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
        backgroundWorker2.RunWorkerAsync();
    }

推荐答案

对于文件的每个单个字节,它正在紧密循环中进行复制,因此更新进度条的速度比UI更新的速度快.您正在用无意义的工作充斥着UI线程.

You're updating the progress bar faster than the UI can update, for every single byte of the file being copied in a tight loop. You're flooding the UI thread with pointless work.

删除 backgroundWorker1 ,无论如何它都没有做任何有用的事情.如果您没有跟踪进度的方法(而 File.Copy 则没有),只需使用没有进度的进度条(将样式设置为Marquee)即可.

Remove backgroundWorker1, it's not doing anything useful anyway. If you don't have a way to track the progress (which you don't with File.Copy), just use a progress bar without progress (set Style to Marquee).

这篇关于为什么WinForm被BackgroundWorker卡住了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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