San*_*ndy 3 c# uilabel winforms progress-bar
我有一个带接口的库.
Public Interface Progress
{
int ProgressValue{get;set;},
string ProgressText{get;set;},
}
Run Code Online (Sandbox Code Playgroud)
库有一个方法Create(虚拟代码):
Public Class TestLibrary
{
Progress _progress;
Public void Create()
{
foreach(var n in TestList)
{
// Do Something
_progress.ProgressValue = GetIndex(n);
_progress.ProgressText = "Updating..." + n;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个引用此库并调用Create方法的项目.它甚至实现了接口进度.
Public Class TestProject : Progress
{
public int ProgressValue
{
get{return progressBar1.Value;}
set{progressBar1.Value = value;}
}
public int ProgressText
{
get{return label1.Text;}
set{label1.Text = value;}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我运行应用程序时,Progress Bar表现正常并正确显示进度,但label1的Text文本根本没有变化.但它在for循环结束时会发生变化并显示循环中的最后一项.任何人都可以帮助我吗?
注意:所有这些代码都是直接编写的,没有经过测试,因为我现在没有我的应用程序.对不起任何语法错误.
听起来你的所有工作都是在UI线程上完成的.不要那样做.
相反,在后台线程中运行循环本身,并使用Control.Invoke
或Control.BeginInvoke
(可能在Progress
实现中)将调用编组到UI线程以更新UI.这将使您的UI在处理时保持响应(并且能够更新标签等).
用过的Label instead of ProgressBar
.你可以尝试这段代码[ using BackGroundWorker
] -
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
private BackgroundWorker _worker;
BusinessClass _biz = new BusinessClass();
public Form3()
{
InitializeComponent();
InitWorker();
}
private void InitWorker()
{
if (_worker != null)
{
_worker.Dispose();
}
_worker = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
_worker.DoWork += DoWork;
_worker.RunWorkerCompleted += RunWorkerCompleted;
_worker.ProgressChanged += ProgressChanged;
_worker.RunWorkerAsync();
}
void DoWork(object sender, DoWorkEventArgs e)
{
int highestPercentageReached = 0;
if (_worker.CancellationPending)
{
e.Cancel = true;
}
else
{
double i = 0.0d;
int junk = 0;
for (i = 0; i <= 199990000; i++)
{
int result = _biz.MyFunction(junk);
junk++;
// Report progress as a percentage of the total task.
var percentComplete = (int)(i / 199990000 * 100);
if (percentComplete > highestPercentageReached)
{
highestPercentageReached = percentComplete;
// note I can pass the business class result also and display the same in the LABEL
_worker.ReportProgress(percentComplete, result);
_worker.CancelAsync();
}
}
}
}
void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
// Display some message to the user that task has been
// cancelled
}
else if (e.Error != null)
{
// Do something with the error
}
}
void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = string.Format("Result {0}: Percent {1}",e.UserState, e.ProgressPercentage);
}
}
public class BusinessClass
{
public int MyFunction(int input)
{
return input+10;
}
}
}
Run Code Online (Sandbox Code Playgroud)
几天前发布在这里