[英]Progress Bar using Background worker between two different classes
這里最最佳的解決方案是什么? 我想使用此變量來指示仿真的進度。
編輯:添加了ALMBerekeningen代碼。 這只是其中的一小部分,完整的代碼太多了,無法在此處顯示。
謝謝!
public class ALMBerekeningen
{
public int sim;
public int Progress;
public double ProgressPerc;
this.ProgressPerc = this.sim / 1000;
this.Progress = (int)Math.Round(this.Progress * 100f, 0, MidpointRounding.AwayFromZero);
}
Public class Form1: Form
{
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
ALMBerekeningen ProgressPerc;
int sims;
sims = (int)ProgressPerc;
try
{
backgroundWorker1.ReportProgress(sims);
}
catch (Exception ex)
{
backgroundWorker1.CancelAsync();
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
lblProgress.Text = "Completed " + progressBar1.Value.ToString() + " %";
progressBar1.Update();
}
}
您需要在啟動時將ALMBerekeningen
實例ALMBerekeningen
給后台工作程序,然后使用事件處理程序中的DoWorkEventArgs.Argument
屬性對其進行訪問:
public void Main()
{
//The instance of the class with the variable for your progress bar
ALMBerekeningen almBerekeningen = new ALMBerekeningen();
BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork += bgw_DoWork;
//Pass your class instance in here
bgw.RunWorkerAsync(almBerekeningen);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//e.Argument is the instance of the class you passed in
var progressPerc = (ALMBerekeningen)e.Argument;
int sims;
sims = progressPerc.ProgressPerc;
try
{
backgroundWorker1.ReportProgress(sims);
}
catch (Exception ex)
{
backgroundWorker1.CancelAsync();
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
附帶地,您顯示的DoWork
處理程序將只執行一次。 我認為您只是為了示例而將其簡化了下來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.