Click here to Skip to main content
15,922,407 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
In my project I want to show the Marquee style progress bar.
where I had a form on which there is a toolbar containing a button.
On the click of that button I call a webservice.
Till webservice call returns I want to show the Marquee progress bar which is on modal dialog.

How I can achieve it by Tasks(TPL) or either way?
Please provide the code sample for it.

What I have tried:

CancellationToken cancelToken = new CancellationToken();
                Task task1 = Task.Factory.StartNew(() =>
                    if (ESignatureService.SignDocuments(documentIds, GlobalVariables.oLoginRO.PersonalSchluessel))
                    breakFlag = false;
                }, cancelToken)
                .ContinueWith(t =>
                    while (oi1ProgressStatus.PercentageComplete < 100)
                        if (oi1ProgressStatus.PercentageComplete == 100)
                            oi1ProgressStatus.PercentageComplete = 0;
                }, TaskScheduler.Default);

I tried by this way but I am getting the following error.

"Controls that were created for a thread that can not be the parent of a control on a different thread."
Updated 19-May-16 8:24am
Gautham Prabhu K 18-May-16 7:57am    
Its Thread affinity issue google on how to use Dispatcher.Invoke method in WPF to avoid this.
Dave Kreskowiak 18-May-16 8:30am    
You cannot create or touch UI controls from anything other than the UI (startup) thread.

You didn't say which UI you were using, WinForms or WPF...
SURAJ-CDAC 18-May-16 8:39am    
I am using Winforms..

The ProgressBar control[^] has built-in support for showing indeterminate progress - just set the Style property[^] to ProgressBarStyle.Marquee:
oi1ProgressStatus.Style = ProgressBarStyle.Marquee;

await Task.Run(() =>
    if (ESignatureService.SignDocuments(documentIds, GlobalVariables.oLoginRO.PersonalSchluessel))
    breakFlag = false;

oi1ProgressStatus.Style = ProgressBarStyle.Blocks;

NB: You can't update UI elements from a background thread, so you'll need to use Invoke to push the updates back onto the UI thread. If you use Task.WaitAny on the UI thread, you'll block the Invoke method, and cause a deadlock. Use async / await instead, which will keep the UI thread responsive whilst your background task is running.

If you can't use async / await, then use a BackgroundWorker[^] to perform your task.
Share this answer

Here's the basic idea, you may have to adapt it to your needs. Don't try to martial threads yourself, that is only necessary with legacy code. The async await keywords provide sufficient abstraction for you not to have to do that.

//This is the button click event handler
//note the async modifier
private async void ReadWebPageEventHandlerAsync(object sender, RoutedEventArgs e)
            string text ;
			//***insert code to start the progress bar here***
            using (var client = new HttpClient())
			     //start the required async process,  in this case it's GetStringAsync
                text = await client.GetStringAsync(@"");

            //control returns here when GetStringAsync finishes, 
			//**insert code to stop the progress bar here**
            //Do something with the result from the await call
             // here a TextBox text is set to the result
            this.textBox.Text = text;
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