为什么WinForm被BackgroundWorker卡住了?

编程入门 行业动态 更新时间:2024-10-23 07:28:36
本文介绍了为什么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卡住了?

本文发布于:2023-11-23 18:35:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:卡住了   WinForm   BackgroundWorker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!