关键词:
c#根据文件的数量来控制进度条,也就是说,复制完一个文件,进度条前进一点,进度条的最大值是文件的数量
说实话,关于进度条的解决方案很多,我暂且假定你在做Winform程序开发。
如果你使用的StatusBar中的进度条的话,你可以不考虑多线程更新UI的问题,因为它本身已经在内部实现了外部线程更新UI控件的逻辑。 但是如果你使用普通的Progressbar控件,那你就得自己处理这部分逻辑,因为控件只能在其所在的UI中更新,如果你想在其它线程中更新那你得用控件上的BeginInvoke方法, 当然还有其它的解决方案。
方案1:
using System;using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
namespace AskAnswers
public class ShowProgressStatus : Form
[STAThread]
static void Main()
Application.Run(new ShowProgressStatus());
public ShowProgressStatus()
InitializeComponent();
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with an editor
/// </summary>
private void InitializeComponent()
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.btnProcess = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
//@design this.TrayHeight = 0;
//@design this.TrayLargeIcon = false;
//@design this.TrayAutoArrange = true;
label1.Location = new System.Drawing.Point(32, 40);
label1.Text = "Progress Value";
label1.Size = new System.Drawing.Size(88, 24);
label1.TabIndex = 2;
progressBar1.Maximum = 10;
progressBar1.Location = new System.Drawing.Point(8, 312);
progressBar1.Minimum = 0;
progressBar1.TabIndex = 0;
progressBar1.Value = 0;
//We have calculated the excat size which will result in only 20 boxes to be drawn
progressBar1.Size = new System.Drawing.Size(520, 40);
progressBar1.Step = 1;
btnProcess.Location = new System.Drawing.Point(152, 168);
btnProcess.Size = new System.Drawing.Size(144, 48);
btnProcess.TabIndex = 1;
btnProcess.Text = "Process";
btnProcess.Click += new System.EventHandler(btnProcess_Click);
textBox1.Location = new System.Drawing.Point(136, 40);
textBox1.Text = "0";
textBox1.TabIndex = 3;
textBox1.Size = new System.Drawing.Size(184, 20);
this.Text = "Display Progress Status";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(616, 393);
this.StartPosition = FormStartPosition.CenterScreen;
this.Controls.Add(textBox1);
this.Controls.Add(label1);
this.Controls.Add(btnProcess);
this.Controls.Add(progressBar1);
void btnProcess_Click(object sender, EventArgs e)
if (isProcessRunning)
MessageBox.Show("A process is already running.");
return;
string[] files = Directory.GetFiles(@"c:\\", "*");
var filesCount = files.Length;
progressBar1.Maximum = filesCount;
Thread.Sleep(50);
Thread backgroundThread = new Thread(
new ThreadStart(() =>
isProcessRunning = true;
for (int n = 0; n < filesCount; n++ )
// 模拟拷贝文件过程
Thread.Sleep(100);
Console.WriteLine(files[n]);
progressBar1.BeginInvoke(
new Action(() =>
progressBar1.Value = n + 1;
));
MessageBox.Show("Thread completed!");
progressBar1.BeginInvoke(
new Action(() =>
progressBar1.Value = 0;
));
isProcessRunning = false;
));
backgroundThread.Start();
private System.Windows.Forms.Button btnProcess;
private System.ComponentModel.Container components;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ProgressBar progressBar1;
private bool isProcessRunning;
方案2: 利用线程池(Task.StartNew())
你只要把btnProcess_Click中的代码替换为下面的代码即可:
void btnProcess_Click(object sender, EventArgs e)if (isProcessRunning)
MessageBox.Show("A process is already running.");
return;
Task<string[]>.Factory.StartNew(() =>
isProcessRunning = true;
return Directory.GetFiles(@"C:\\", "*");
)
.ContinueWith(files =>
string[] filesResult = files.Result;
progressBar1.Maximum = filesResult.Length;
Console.WriteLine("The Maximum of Progress Bar " + progressBar1.Maximum);
return filesResult;
)
.ContinueWith(files =>
string[] filesResult = files.Result;
Console.WriteLine("The files count " + filesResult.Length);
for (int n = 0; n < filesResult.Length; n++ )
// 模拟拷贝文件过程
Thread.Sleep(100);
Console.WriteLine(filesResult[n]);
progressBar1.Value = n + 1;
)
.ContinueWith(files =>
MessageBox.Show("Thread completed!");
progressBar1.BeginInvoke(
new Action(() =>
progressBar1.Value = 0;
));
isProcessRunning = false;
);
当然,你也可以通过BackgroundWorker来做,可能更简单一点儿,原理相同,你可以搜索一下相关方案。
注意,我只是通过一个Thread.Sleep(100)来模拟你Copy文件的逻辑。
追问BackgroundWorke这个怎么做呢?给个例子呗
参考技术A 1. 先计算出文件总数量为 nMaxprogressBar1.Maximum = nMax;
2. 复制完一个文件+1
progressBar1.Value++
文件上传进度条干扰控制器重定向
...是,控制进度条的javascript会干扰我的控制器,我不知道如何修复它。我的控制器从不使用链接重定向回家(它在没有实现进度条的情况下工作)。删除window.locatio 查看详情
c#控制台console进度条(代码片段)
1说明笔者大多数的开发在Linux下,多处用到进度条的场景,但又无需用到图形化界面,所以就想着弄个console下的进度条显示。2步骤清行显示//清行处理操作intcurrentLineCursor=Console.CursorTop;//记录当前光标位置Console.SetCursorPosition(0,Co... 查看详情
c#做一个播放音频文件的进度条该怎么实现呢??
当点击播放的时候根据音频文件的时间动态显示进度,用TrackBar不知道可不可以,有没高手给个详细的代码实现,万分感谢...没办法,几乎所有的timer有会有一定的时间误差。不过听你的意思貌似是根据timer的计算来更新进度条的数据... 查看详情
c#通过线程来控制进度条(转)--讲解多线程对界面的操作
//通过创建委托解决传递参数问题privatevoid_btnRun_Click(objectsender,System.EventArgse){RunTaskDelegaterunTask=newRunTaskDelegate(RunTask);//委托同步调用方式runTask(Convert.ToInt16(_txtSecond.Value));}//通过创建委托解决传递参数问题,通过委托的 查看详情
c#绑定datagridview时显示进度条
...ble我是通过表直接绑定,我想在绑定的同时进度条也同时根据比例进行显示。创建一个模板列,然后用进度条控件,进度条的数据格式是0~1的浮点吧,数据库里格式别错了参考技术A数据量有多大追问有上万。在用表直接绑定到da... 查看详情
如何根据字段合并两个 CSV 文件并在每条记录上保持相同数量的属性?
】如何根据字段合并两个CSV文件并在每条记录上保持相同数量的属性?【英文标题】:HowdoImergetwoCSVfilesbasedonfieldandkeepsamenumberofattributesoneachrecord?【发布时间】:2014-06-1403:59:55【问题描述】:我正在尝试根据每个文件中的特定字... 查看详情
如何使用 C# HttpClient PostAsync 显示上传进度
】如何使用C#HttpClientPostAsync显示上传进度【英文标题】:HowtodisplayuploadprogressusingC#HttpClientPostAsync【发布时间】:2016-02-1016:11:11【问题描述】:我正在使用XamarinPCL创建适用于Android和iOS的文件上传应用程序,并且我已设法实现文件... 查看详情
vc6.0里mfc进度条如何使用
参考技术A演练CProgress7.1进度条的主要功能进度条控制(ProgressControl)主要用来进行数据读写、文件拷贝和磁盘格式等操作时的工作进度提示情况,如安装程序等,伴随工作进度的进展,进度条的矩形区域从左到右利用当前活动... 查看详情
c#进度条怎么用/c#progressbar的用法
...。设置日志输出类可以在文本框中输入过程日志。生成exe文件进行测试在进度条长度框中输入100,点击【开始】,进度条会持续前进。点击【暂停】,进度条会停止前进,【暂停】按钮上的文字会显示为【继续】。再点击【继续... 查看详情
SwiftUI:如何根据滚动视图的用户当前滚动位置同步/更改进度条进度?
】SwiftUI:如何根据滚动视图的用户当前滚动位置同步/更改进度条进度?【英文标题】:SwiftUI:HowdoIsync/changeprogressbarprogressbasedonscrollview’susercurrentscrollposition?【发布时间】:2021-02-1822:19:56【问题描述】:我在ScrollView中有水平进... 查看详情
如何根据嵌套循环中的多个变量报告进度(对于进度条)?
】如何根据嵌套循环中的多个变量报告进度(对于进度条)?【英文标题】:HowcanIreportprogressbasedonmultiplevariablesinanestedloop(foraprogressbar)?【发布时间】:2017-04-2922:28:48【问题描述】:我有一个BackgroundWorker和一个ProgressBar。工作时... 查看详情
C#任务栏中的Windows 7进度条?
...题描述】:如果您在Windows7测试版中注意到,如果您复制文件或其他系统操作,任务栏中的Windows资源管理器图标将填满一个绿色进度条,相当于表单上的进度条。有没有办法在我的C#表单中强制我的任务栏进度条与我正在执行的... 查看详情
如何根据值更改 ant design 进度条颜色?
】如何根据值更改antdesign进度条颜色?【英文标题】:HowcanIchangeantdesignprogressbarcoloraccordingtovalue?【发布时间】:2020-08-2804:58:39【问题描述】:我想给“黄色”颜色直到50%,在50%之后它会显示“绿色”。它不像strokeColor功能。我想... 查看详情
C# 自定义控件(圆形进度条) Xamarin Forms
...东西的最佳方法:我从来没有创造过这样的东西。我知道如何使用进度条,但不知道“圆形进度条”感谢您的帮助和任何提示。编辑:如果你有一个插件/nuget来做到这一点,那很 查看详情
如何更新进度条 OnSelectedIndexChanged?
】如何更新进度条OnSelectedIndexChanged?【英文标题】:HowdoIupdateaprogressbarOnSelectedIndexChanged?【发布时间】:2009-06-1105:38:21【问题描述】:我正在使用.NET(C#)开发一个Windows应用程序,并且我有一个带有一些列表项的DropDownList。在事件... 查看详情
你如何在 Windows 窗体上显示动画 GIF (c#)
】你如何在Windows窗体上显示动画GIF(c#)【英文标题】:HowdoyoushowanimatedGIFsonaWindowsForm(c#)【发布时间】:2010-09-1500:14:16【问题描述】:我有一个显示进度消息的表单,因为进程运行时间相当长。这是对Web服务的调用,所以我无法在... 查看详情
控制进度条的粗细
...】:我有一个进度条,如下所示:我的问题很简单,我将如何控制圆形视图的“厚度”,因为在我看来它似乎太厚了。非常感谢。【问题讨论】:使用自定义进度对话框,我的意思是你可以使用自己的图像,否则你无法控制默认... 查看详情
如何更新进度条使其平滑增加?
】如何更新进度条使其平滑增加?【英文标题】:Howtoupdateaprogressbarsoitincreasessmoothly?【发布时间】:2013-01-0707:01:20【问题描述】:我使用WPF(C#)的进度条来描述进程的进度。我的算法如下:DoSomethingCode1();ProgressBar.SetPercent(10);//10%Do... 查看详情