Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Have used to display the progress Bar using thread concept when load data to data grid in WPF. the following codes are used.
public partial class MainWindow : Window
    {
        private  bool firstLoad = true;      
        private delegate void SimpleDelegate();
        DataSet ds = new DataSet();
        CollectionView view;
        public MainWindow()
        {
           InitializeComponent();
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(RunOnBGThread);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGThreadWorkDone);
            worker.RunWorkerAsync();
            BackgroundWorker worker1 = new BackgroundWorker();
            worker1.DoWork += new DoWorkEventHandler(RunOnBGThreadProgressBas);
            worker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGThreadWorkDone);
            worker1.RunWorkerAsync();		
        }
        private void RunOnBGThread(object sender, DoWorkEventArgs e)
        {
            LoadProcess();
        }       
        private void LoadProcess()
        {
            try
            {
                SimpleDelegate del1 = delegate()
                {                                        
                    if (firstLoad)
                    {
                         
                        firstLoad = false;                       
                        SqlHelper.FillDataset(Application.Current.Properties["constr"].ToString(), CommandType.Text, "select * from account a left join transscheme ts on ts.schemecode = a.scheme where dist_code='c' order by acct_code", ds, new String[] { "tab_grdbindload" });
                         this.DataContext = ds.Tables["tab_grdbindload"];
                         view = (CollectionView)(CollectionViewSource.GetDefaultView(ds.Tables["tab_grdbindload"]));
                         grd_boview.ItemsSource = view;    
                     
                    }                    
                };
                this.Dispatcher.BeginInvoke(DispatcherPriority.Send, del1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }		
        }
        private void BGThreadWorkDone(object sender, RunWorkerCompletedEventArgs e)
        {
            prgbar.Visibility = Visibility.Collapsed;  
        }
        private void RunOnBGThreadProgressBas(object sender, DoWorkEventArgs e)
        {
            Duration dd = new Duration(new TimeSpan(10));  //creating an object of the duration class.
            DoubleAnimation da = new DoubleAnimation(30.0, dd); 
           prgbar.BeginAnimation(ProgressBar.ValueProperty, da);
        }
    }
}

In this above code the following error was thrown "The calling thread cannot access this object because a different thread owns it." the line of code "prgbar.BeginAnimation(ProgressBar.ValueProperty, da);".
Please advice me how to use the thread while load the progress bar.
Posted
Updated 17-Jul-11 21:14pm
v2

1 solution

This method also should not be called on you non-UI thread but invoked on the UI thread using Dispatcher.Invoke or Dispatcher.BeginInvoke. You cannot call anything on UI from non UI thread.

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

[EDIT]
This is how to work with delegate needed for invocation properly:

C#
void UpdateProgressBar(Dispatcher dispatcher, ProgressBar progressBar, double value) {
dispatcher.BeginInvoke(
    new Action<ProgressBar, double>(
        (progressBarInstance, newValue) => {
            progressBarInstance.Value = newValue;
        }), progressBar, value);
} //UpdateProgressBar


The body of the anonymous delegate instance comes between "{}" above.

—SA
 
Share this answer
 
v3
Comments
lakshmi.ksangam 18-Jul-11 3:23am    
Thanks for ur reply..

Have used Dispatcher.BeginInvoke. but progress bar was not updated. for ur reference below mention the code..

private void RunOnBGThreadProgressBas(object sender, DoWorkEventArgs e)
{
SimpleDelegate del2 = delegate()
{
Duration dd = new Duration(new TimeSpan(10));
DoubleAnimation da = new DoubleAnimation(30.0, dd);
prgbar.BeginAnimation(ProgressBar.ValueProperty, da);


};
this.Dispatcher.BeginInvoke(DispatcherPriority.Send, del2);
}
Sergey Alexandrovich Kryukov 18-Jul-11 11:09am    
I did not check it up, not sure I have all the data, but you don't need animation. Just assign Value the the value of your progress.
Also, I don't see invocation. Your work with delegates is very cumbersome.
--SA
Sergey Alexandrovich Kryukov 18-Jul-11 11:23am    
Please see the code sample above, after [EDIT].
--SA
Espen Harlinn 18-Jul-11 8:37am    
Good reply, my 5
Sergey Alexandrovich Kryukov 18-Jul-11 11:04am    
Thank you, Espen.
--SA

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