Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey I'm just looking for a few suggestions as which way to handle a specific task in my application that takes a very long time.
Ok the task is to upload a large chunk of data which takes a couple of minutes at a time. What I want to do is let the user continue doing seperate tasks while this other task is taking place, I have thought about putting a backgroung worker and a progress bar but there are no clear concise examples on the net. Anyway im open to any suggestions and any help is very much apreciated.
Posted
Comments
Herman<T>.Instance 23-Oct-12 3:51am    
what doe this task technically do?
frostcox 23-Oct-12 3:56am    
Task loads data from a file and then inserts in to a database.
Herman<T>.Instance 23-Oct-12 5:22am    
what type of database?

A background worker is very, very easy to do:
Create a BackgroundWorker instance, and hook it to a couple of events:
C#
private BackgroundWorker worker = new BackgroundWorker();
...
butDoMyTask.Enabled = false;
ShowProgress.Visible = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.WorkerReportsProgress = true;
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();
Then in the events, you just have to show progress, do the actual work, and mark when it completes:
C#
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    butDoMyTask.Enabled = true;
    ShowProgress.Visible = false;
    }

void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
    ShowProgress.Value = e.ProgressPercentage;
    }

void worker_DoWork(object sender, DoWorkEventArgs e)
    {
    for (int i = 0; i <= 100; i += 10)
        {
        Thread.Sleep(1000);
        worker.ReportProgress(i);
        }
    }
 
Share this answer
 
Comments
frostcox 23-Oct-12 4:07am    
Thanks very much for your reply, only issue I have is that in the worker_DoWork method it's not possible to do the loop to report the progress as this is where I will need to upload the data. Thanks
OriginalGriff 23-Oct-12 4:12am    
What are you doing to upload the data?
frostcox 23-Oct-12 4:14am    
ok the app has a datagrid which loads the data from a file, when the user is happy with the details of the grid they press save then a whole lot of validation takes place and then inserts in to the relevent tables in the database.
OriginalGriff 23-Oct-12 4:18am    
So which part takes the time?
frostcox 23-Oct-12 4:22am    
The validation, it goes in to 4 different tables for each record in the grid, the grid could have 1000's of records at a tile so this is whats taking the time.
You were right! Use background worker there is simple step:
1. Start progress bar
2. BackgroundWorker1.RunWorkerAsync() [where evere you want to put]
3. On BackgroundWorker1_DoWork Event, do the data loading thing.
4. On BackgroundWorker1_RunWorkerCompleted event , stop Progress bar.
Caution: Don’t go cross thread. It will give error.
 
Share this answer
 
Comments
frostcox 23-Oct-12 4:19am    
Hey what you mean by BackgroundWorker1.RunWorkerAsync() [where evere you want to put]
do I not need to do that in the Constructor?

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