1
我創建了一個控制檯應用程序,需要大約5分鐘的時間才能運行。主要方法需要大約10個方法來完成這項工作。將控制檯應用程序轉換爲帶有進度條的窗體?
如何將控制檯應用程序更改爲Windows Forms應用程序,以更新進度條/顯示通過這10種方法的最新進度?
非常感謝!
我創建了一個控制檯應用程序,需要大約5分鐘的時間才能運行。主要方法需要大約10個方法來完成這項工作。將控制檯應用程序轉換爲帶有進度條的窗體?
如何將控制檯應用程序更改爲Windows Forms應用程序,以更新進度條/顯示通過這10種方法的最新進度?
非常感謝!
您可以在Winforms中使用BackgroundWorker
組件。只需複製粘貼此代碼即可。我已經使用了Label instead of ProgressBar
。隨着任務在後臺進展,標籤將更新爲percentage completion
。
耗時的方法/任務必須在Do_Work
處理程序中調用。運行下面的示例。
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();
}
/// Do the time consuming work here
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;
}
}
}