0

How should I know if an application is working or processing something? Lets say that I'm trying to write a huge data into a file, on that time the application is not responding. I want to know the application current status.

4

1 回答 1

5

Use BackgroundWorker componenet to process huge data in background thread. Notify user about progress via ProgressChanged event.

Sample:

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");
    }
}
于 2012-10-20T19:13:53.427 回答