我应该如何知道应用程序是否正在运行或处理某些内容?让我们说我正在尝试将大量数据写入文件,那时应用程序没有响应。我想知道应用程序的当前状态。
答案 0 :(得分:5)
使用BackgroundWorker组件处理后台线程中的大数据。通过ProgressChanged活动通知用户进度。
样品:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
}
private void WriteDataButton_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= numberOfIterations; i++)
{
// write part of data
backgroundWorker1.ReportProgress(i * 100 / numberOfIterations);
}
}
// This event handler updates the progress.
private void backgroundWorker1_ProgressChanged(
object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
// This event handler deals with the results of the background operation.
private void backgroundWorker1_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Huge data was written");
}
}