Click here to Skip to main content
15,742,619 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am Yogesh Sharma. I am working with progressbar in Windows Forms using C#. I need to open a Corel Draw file(.cdr) from my C# code. It takes about 10 seconds to do this task, hence I want to use ProgressBar. I am trying it with BackgroundWorker. My C# code is as follows...

private void button1_Click(object sender, EventArgs e)
    progressBar1.Maximum = 100;
    progressBar1.Step = 1;
    progressBar1.Value = 0;
    CorelDRAW.Application cdr = new CorelDRAW.Application();
    cdr.OpenDocument(@"d:\xxx.cdr", 1);
    cdr.AppWindow.WindowState = cdrWindowState.cdrWindowMaximized;

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    BackgroundWorker worker = sender as BackgroundWorker;

    for (int i = 1; i <= 10; i++)
        // Perform a time consuming operation and report progress.
        worker.ReportProgress(i * 10);

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    progressBar1.Value = e.ProgressPercentage;

My problem is that when I click on button1, ProgressBar doesn'n show any progress. And when the task is completed(i:e cdr file gets opened), progressbar shows the full progress immediately. I need to show the progress during the process of opening cdr file.

Please help what I am missing here or what is wrong with my code?
Any help will be appreciated.
Updated 16-Jan-13 22:55pm

1 solution

Your problem is that you're opening .cdr file on main (GUI) thread.
Although you have background worker running in background and reporting progress, GUI thread is busy opening file and can't update progress.
(All window controls have to be updated from GUI thread, otherwise exception is thrown)

You need to move file open code to background worker if you want interface to remain responsive.
Share this answer
yogesh a sharma 17-Jan-13 6:37am    
thanks sir for reply .. Here i need to open cdr file from dowork event(background worker)..
yes I got it.. but what else i have to do here??? please state with example...
i opened cdr file from background worker but file opens first and then progress bar is showing the progress..please help sir....
sjelen 17-Jan-13 7:08am    
That's because you wrote it like that. You're not reporting real progress, just simulating it.
cdr.OpenDocument obviously works synchronous, so while it works your loop for 'progress reporting' is not executed yet.
I don't know what library you use and what is CorelDRAW.Application object, so I can't help further.
You can report real progress only if this object supports that. Does it have some events you can attach to?

Or you can cheat :) : have 2 background workers, one to open the document, the other to fake progress.
yogesh a sharma 17-Jan-13 8:04am    
thanks sir having 2 background workers is working fine...thanks....a lot...

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