C# 我怎样才能让它在这个时候起作用;响应进度条“;?
早上好,我正在尝试编写一个应用程序,在他的界面中使用progressbar(C#,WPF)。我已经了解到需要使用Backgroundworker在不同的线程中执行UI任务。我试图使用大量信息使其工作,但什么也没有发生(程序工作正常,但progressbar仅显示在“艰苦工作任务”的末尾) 我是土木工程师(不是软件工程师),所以我问是否有人能帮我C# 我怎样才能让它在这个时候起作用;响应进度条“;?,c#,wpf,C#,Wpf,早上好,我正在尝试编写一个应用程序,在他的界面中使用progressbar(C#,WPF)。我已经了解到需要使用Backgroundworker在不同的线程中执行UI任务。我试图使用大量信息使其工作,但什么也没有发生(程序工作正常,但progressbar仅显示在“艰苦工作任务”的末尾) 我是土木工程师(不是软件工程师),所以我问是否有人能帮我 namespace SAP2000___Quake_Definitions { /// <summary> /// Interaction
namespace SAP2000___Quake_Definitions
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly BackgroundWorker bgWoker = new BackgroundWorker();
public MainWindow()
{
InitializeComponent();
this.bgWoker.WorkerReportsProgress = true;
this.bgWoker.WorkerSupportsCancellation = true;
this.bgWoker.DoWork += bgWorker_DoWork;
this.bgWoker.ProgressChanged += bgWorker_ProgressChanged;
this.bgWoker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
}
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progBar.Value = e.ProgressPercentage;
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgWorker = (BackgroundWorker)sender;
Dispatcher.Invoke(new Action(() => DoTheHardWork()));
}
private void processButton_Click(object sender, RoutedEventArgs e)
{
this.bgWoker.RunWorkerAsync();
}
private void DoTheHardWork()
{
switch (this.chckBox2.IsChecked.GetValueOrDefault())
{
case true:
this.bgWoker.ReportProgress(0);
//more hardwork with inputs from WPF
case false:
this.bgWoker.ReportProgress(0);
//more hardwork with inputs from WPF
}
}
}
}
名称空间SAP2000\uuuuuuuu\u定义
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
private readonly BackgroundWorker bgWoker=new BackgroundWorker();
公共主窗口()
{
初始化组件();
this.bgWoker.WorkerReportsProgress=true;
this.bgWoker.workerSupportsScanCellation=true;
this.bgWoker.DoWork+=bgWorker\u DoWork;
this.bgWoker.ProgressChanged+=bgWorker\u ProgressChanged;
this.bgWoker.RunWorkerCompleted+=bgWorker\u RunWorkerCompleted;
}
私有void bgWorker\u ProgressChanged(对象发送方,ProgressChangedEventArgs e)
{
this.progBar.Value=e.ProgressPercentage;
}
私有void bgWorker\u DoWork(对象发送方,DoWorkEventArgs e)
{
BackgroundWorker bgWorker=(BackgroundWorker)发送方;
调用(新操作(()=>DoTheHardWork());
}
私有无效处理按钮\单击(对象发送方,路由目标)
{
this.bgWoker.RunWorkerAsync();
}
私人工作
{
开关(this.chckBox2.IsChecked.getValuerDefault())
{
大小写正确:
此.bgWoker.ReportProgress(0);
//通过WPF的投入,更加努力地工作
案例错误:
此.bgWoker.ReportProgress(0);
//通过WPF的投入,更加努力地工作
}
}
}
}
您不应该这样使用BackgroundWorker。几年前我写了一些示例代码。这会让你走上正确的轨道:
#region Primenumbers
private void btnPrimStart_Click(object sender, EventArgs e)
{
if (!bgwPrim.IsBusy)
{
//Prepare ProgressBar and Textbox
int temp = (int)nudPrim.Value;
pgbPrim.Maximum = temp;
tbPrim.Text = "";
//Start processing
bgwPrim.RunWorkerAsync(temp);
}
}
private void btnPrimCancel_Click(object sender, EventArgs e)
{
if (bgwPrim.IsBusy)
{
bgwPrim.CancelAsync();
}
}
private void bgwPrim_DoWork(object sender, DoWorkEventArgs e)
{
int highestToCheck = (int)e.Argument;
//Get a reference to the BackgroundWorker running this code
//for Progress Updates and Cancelation checking
BackgroundWorker thisWorker = (BackgroundWorker)sender;
//Create the list that stores the results and is returned by DoWork
List<int> Primes = new List<int>();
//Check all uneven numbers between 1 and whatever the user choose as upper limit
for(int PrimeCandidate=1; PrimeCandidate < highestToCheck; PrimeCandidate+=2)
{
//Report progress
thisWorker.ReportProgress(PrimeCandidate);
bool isNoPrime = false;
//Check if the Cancelation was requested during the last loop
if (thisWorker.CancellationPending)
{
//Tell the Backgroundworker you are canceling and exit the for-loop
e.Cancel = true;
break;
}
//Determin if this is a Prime Number
for (int j = 3; j < PrimeCandidate && !isNoPrime; j += 2)
{
if (PrimeCandidate % j == 0)
isNoPrime = true;
}
if (!isNoPrime)
Primes.Add(PrimeCandidate);
}
//Tell the progress bar you are finished
thisWorker.ReportProgress(highestToCheck);
//Save Return Value
e.Result = Primes.ToArray();
}
private void bgwPrim_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pgbPrim.Value = e.ProgressPercentage;
}
private void bgwPrim_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pgbPrim.Value = pgbPrim.Maximum;
this.Refresh();
if (!e.Cancelled && e.Error == null)
{
//Show the Result
int[] Primes = (int[])e.Result;
StringBuilder sbOutput = new StringBuilder();
foreach (int Prim in Primes)
{
sbOutput.Append(Prim.ToString() + Environment.NewLine);
}
tbPrim.Text = sbOutput.ToString();
}
else
{
tbPrim.Text = "Operation canceled by user or Exception";
}
}
#endregion
#区域素数
私有无效btnPrimStart\单击(对象发送者,事件参数e)
{
如果(!bgwPrim.IsBusy)
{
//准备进度条和文本框
int temp=(int)nudPrim.Value;
pgbPrim.最大值=温度;
tbPrim.Text=“”;
//开始处理
bgwPrim.RunWorkerAsync(临时);
}
}
私有无效btnprimmcancel\u单击(对象发送者,事件参数e)
{
如果(bgwPrim.IsBusy)
{
bgwPrim.CancelAsync();
}
}
私有void bgwPrim_DoWork(对象发送方,DoWorkEventArgs e)
{
int highestToCheck=(int)e.参数;
//获取运行此代码的BackgroundWorker的引用
//用于进度更新和取消检查
BackgroundWorker thisWorker=(BackgroundWorker)发件人;
//创建存储结果并由DoWork返回的列表
列表素数=新列表();
//检查1和用户选择的任何上限之间的所有不均匀数
对于(int PrimeCandidate=1;PrimeCandidate
您必须将所有UI编写工作限制在进度报告中,并运行wokrer compelte事件。这些将在创建BGW的线程中自动提升(应该是UI线程)
请注意,您只能报告不同步骤之间的进度。我有一个优势,那就是无论如何我都必须编写循环。但如果您有现有代码(如大多数下载或磁盘代码),通常只能在文件之间报告。我的错误有三个:
解决了第2点和第3点,UI对“努力工作”功能(后台运行)的响应非常完美。我不是WPF的人,但我认为你不应该在工作中调用。这将把工作推到UI线程,因此您将失去