Click here to Skip to main content
15,905,004 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
I am doing a c# windows application. I have to do some lengthy work just like many tables data update one by one so i just want to show which database updation is going on. Here i gave a sample program to solve my problem.

C#
for (int i = 0; i < 10; i++)
            {
                richTextBox1.Text += "Now" + i + " is running \n";
                System.Threading.Thread.Sleep(1000);
            }
Posted
Comments
lewax00 30-Jul-12 11:50am    
And your question is what exactly?
[no name] 30-Jul-12 12:10pm    
I am not sure what you are asking here either.

I think what you want is to use the BackgroundWorker:

C#
var bw = new BackgroundWorker();
bw.DoWork += (sender1, eventArgs1) =>
                        {
                            var bw1 = (BackgroundWorker)sender1;
                            System.Threading.Thread.Sleep(1000);
                            bw1.ReportProgress(1);
                        };
bw.ProgressChanged += (sender2, eventArgs2) =>
            {
                            richTextBox1.Text += "Now" + i + " is running \n";
            };
bw.RunWorkerAsync() 


I would use a Task with the Dispatcher in WPF, but the above will work with WinForms.

Forgot the start of the Background worker
 
Share this answer
 
v2
I'm not really sure what you are asking, but I think you might want something like this:

C#
int currentIndex = 0;
String[] updates;      // the array containing all the updates you are doing
                       // as Strings since you didn't specify

doUpdates();           // call this when you want to get started

void doUpdates()
{
    // check that current index is in bounds first, then...

    using(BackgroundWorker bw = new BackgroundWorker)
    {
        richTextBox1.Text += "Now " + i + " is running\n";

        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_Completed);
        bw.RunWorkerAsync();
    }
}

void bw_DoWork(object sender, DoWorkEventArgs e)
{
    // do your updates on updates[currentIndex]
}

void queryWorker_Completed(object sender, RunWorkerCompletedEventArgs e)
{
    ++currentIndex;
    doUpdates();
}


This will do each update on a BackgroundWorker thread, then update to the next index and run through the rest of the updates.
 
Share this answer
 
Use a progress bar[^], it's much better for the user experience.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900