Question

I am working on an application which loads an OLE Database into a DataGridView. Although the database file is locally stored, it takes some time for the application to load the databse, so I only get a "loading" cursor. I want some more: I want to create a progress bar to show while the db is loading and to hide when the db is completely loaded.

I searched on Google, but I do not find what I am looking for. What can I do?

(I am working in Visual Studio, so keep in mind that the whole code for the dataset is automatically written by the IDE)

Was it helpful?

Solution

You are probably looking for a BackgroundWorker used in conjunction with a ProgressBar put both on your form and use the following code:

public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
    Shown += new EventHandler(Form1_Shown);

    // To report progress from the background worker we need to set this property
    backgroundWorker1.WorkerReportsProgress = true;
    // This event will be raised on the worker thread when the worker starts
    backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
    // This event will be raised when we call ReportProgress
    backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}
void Form1_Shown(object sender, EventArgs e)
{
    // Start the background worker
    backgroundWorker1.RunWorkerAsync();
}
// On worker thread so do our thing!
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // Your background task goes here
    for (int i = 0; i <= 100; i++)
    {
        // Report progress to 'UI' thread
        backgroundWorker1.ReportProgress(i);
        // Simulate long task
        System.Threading.Thread.Sleep(100);
    }
}
// Back on the 'UI' thread so we can update the progress bar
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // The progress percentage is a property of e
    progressBar1.Value = e.ProgressPercentage;
}

}

This is just an example of how to use it. You will need to modify the:

backgroundWorker1.ReportProgress(i);

So that it actually is reporting progress pertaining to what is happening with the OLE Database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top