Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello Friends,

I want to show progress bar on WPF Window.
and I want to update its value by click on one Button.

I have tried background worker but it is not is of no use.

Please help to implement simple method by which I can update values/texts on UI Form.

I have below code till now -

 private void btn_Upload_Click(object sender, RoutedEventArgs e)
        {           
            Proc_Bar.Value = 0;
            BackgroundWorker worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerAsync();

        }


  void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            testProcBar();
            
        }


private void testProcBar()
        {
         

            Thread.Sleep(3000);
            Proc_Bar.Value = Proc_Bar.Value + 1;
            Thread.Sleep(3000);
            Proc_Bar.Value = Proc_Bar.Value + 1;
Thread.Sleep(3000);
            Proc_Bar.Value = Proc_Bar.Value + 1;
Thread.Sleep(3000);
            Proc_Bar.Value = Proc_Bar.Value + 1;
Thread.Sleep(3000);
            Proc_Bar.Value = Proc_Bar.Value + 1;
Thread.Sleep(3000);
            Proc_Bar.Value = Proc_Bar.Value + 1;
Thread.Sleep(3000);
            Proc_Bar.Value = Proc_Bar.Value + 1;


        }


Please help.!!!
Posted
Comments
Philippe Mori 27-Aug-15 12:20pm    
You are not using the background worker correctly. At least, you should call ReportProgress to update the progress bar. It should not be jard to find a lot of example on the web.

Here is a my solution (with cancelling support). Button is named "Start", Progress bar is named "Progress1".

C#
private CancellationTokenSource _cancellation;

private async void Start_Click( object sender, RoutedEventArgs e )
{
    if ( _cancellation != null )
    {
        _cancellation.Cancel( false );
        _cancellation.Dispose();
    }
    _cancellation = new CancellationTokenSource();

    try
    {
        Debug.WriteLine(
          "Start_Click: My Thread id is {0}",
          Thread.CurrentThread.ManagedThreadId );

         // call it in another thread
         await Task.Run(
             () => AsyncUpdateProgressBar( _cancellation.Token ),
             _cancellation.Token )
           .ConfigureAwait( false );

         // call it in same thread
         //await AsyncUpdateProgressBar(_cancellation.Token);
     }
     catch ( TaskCanceledException )
     {
         // ignore
     }
 }

 private async Task AsyncUpdateProgressBar( CancellationToken token )
 {
     Debug.WriteLine(
       "AsyncUpdateProgressBar: My Thread id is {0}",
       Thread.CurrentThread.ManagedThreadId );

     for ( var i = 0; i <= 100 & !token.IsCancellationRequested; i++ )
     {
         // update value on dispatcher async
         // this is necessary, when calling this method from another thread
         // than the user interface thread!!!
         var tempLocal = i;
         await Dispatcher.InvokeAsync( () => Progress1.Value = tempLocal );

         // wait a little while
         await Task.Delay( 100, token );
     }

 }
 
Share this answer
 
Comments
Pravinkarne.31 27-Aug-15 6:28am    
Thanks for the reply...Want to know further..

Is there any simple way in WPF application by which ..when I click on button my progress bar will be updated and won't be non-responsive.
Like Windows Form Application - we can use -
checkforillegalcrossthreadcalls = False;
Frank Lindecke 27-Aug-15 7:39am    
Have a look at https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher(v=vs.110).aspx. Every DispatcherObject (the base class of all WPF controls) provides the Dispatcher property.

Dispatcher class provides method CheckAccess() which can be used for your purpose.
Pravinkarne.31 28-Aug-15 4:01am    
Thank you!!!
Create class variable as
C#
private readonly SynchronizationContext context = null;


initialize that in constructor :
C#
this.context =  SynchronizationContext.Current;


Now whenever update the UI Thread us it like below :
C#
this.context.Send(x => Proc_Bar.Value = Proc_Bar.Value + 1;, null);


This might help, try this.
 
Share this answer
 
Comments
Pravinkarne.31 28-Aug-15 4:01am    
Thanks..
After changing to
this.context.Send(x => Proc_Bar.Value = Proc_Bar.Value + 1, null);
working correctly.

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