Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Threading...

Status
Not open for further replies.

alexjamesbrown

Programmer
Mar 7, 2007
8
0
0
GB
Hi,

I've got a windows application that references a class library -

in my class library i have a method that returns a large collection.

on the windows form, i have a button, that calls the method.... the method takes about a minute or so to do what it needs to do (its a large operation)
when the button is pressed, the UI locks up - how can i make this happen on a different thread, and also, is it possible to add a progress bar to show how far along the operation is?

regards,

alex
 
You may find it useful to use BackgroundWorker component. Some tips on how to use it would be:
1. Drop a BackgroundWorker component on your form
2. Add your large operation routine in the DoWork event of BackGroundWorker
3. Use ReportProgress routine of the background worker to report progress
4. Add code to update visual components that show progress in ProgressChanged event of BackgroundWorker

5. Add code to update visual components when the work is completed in the BackgroundWorker RunWorkerCompleted event


The code below is a simple example that i tried:
Code:
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //This event will be called on another thread when background worker is started 

            //Lets process some elements 
            for (int i = 0; i < 60; i++)
            {
                //Simulate long time processing by sleeping 
                Thread.Sleep(1000);

                //Update background worker progress (this will call ProgressChanged event)
                backgroundWorker1.ReportProgress(i);
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //This event will be called everytime the background worker reports progress

            //E.g. step up the progress bar and show a number on the label
            progressBar1.Value = e.ProgressPercentage;

            label1.Text = "Processing element " + e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //This event will be called when the background worker DoWork routine is completed
            label1.Text = "Background work is completed";
        }

        private void button1_Click(object sender, EventArgs e)
        {   
            //Make sure the background worker is set to report progress (it can also be set on properties at design time)
            backgroundWorker1.WorkerReportsProgress = true;

            //Start the background work (this will call DoWork event on another thread) 
            backgroundWorker1.RunWorkerAsync();
        }

You can find more help about BackgroundWorker on the following link:

Hope this helps.

"It is in our collective behaviour that we are most mysterious" Lewis Thomas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top