Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hello All

I have to refresh a DataGrid after every 10 mints interval, So I am using a timer and set the interval ....

But now, I getting an Error when next time grid is filling.

Error Msg : The calling thread cannot access this object because a different thread owns it.

Code
C#
void loadGrid()
       {
           DataTable tabDeshbrd = new DataTable();
           clsBL objBl = new clsBL();
           tabDeshbrd = objBl.GetDataTable();

           ICollectionView employeesView = CollectionViewSource.GetDefaultView(tabDeshbrd);
           employeesView.GroupDescriptions.Add(new PropertyGroupDescription("Status"));

           EmployeesDataGrid.DataContext = employeesView;
       }

private void timer1_Tick(object sender, EventArgs e)
       {
           loadGrid();
       }


I am getting error in this statment: EmployeesDataGrid.DataContext = employeesView.
Error Mag : The calling thread cannot access this object because a different thread owns it.

Please help me ....

Regards,
Vijay
Posted

That happen because you try to set a Dependency Property in another thread (not the UI thread). You can fix it by setting the DataContext property in the UI thread. Something like:


C#
void loadGrid()
{
    //...

    Dispatcher.BeginInvoke(
        new ThreadStart(() => EmployeesDataGrid.DataContext = employeesView));
}
 
Share this answer
 
Comments
P.S Vijay 27-Feb-13 1:17am    
Thanks for your support .... It is working fine .... :)
Marco Bertschi 23-Nov-13 14:04pm    
Thank you very much for this, you have just solved a problem of mine :D
Chethan T R 26-Sep-14 1:41am    
Dispatcher doesn't work in console application, if i use thread.start() method i am getting an error "The calling thread cannot access this object because a different thread owns it." Please suggest!
Shmuel Zang 30-Sep-14 2:18am    
Dispatcher is a property of the DispatcherObject class - The UI elements of WPF are descendants of that class. For more information about synchronizing to a thread, you can read the MSDN article about: SynchronizationContext.
The problem is: your timer event handler is executed in a thread other then your UI thread. And this is actually a good thing.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

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[^].

Also consider using my schema for polling a database by timer based on a combination of a timer and a thread; this solution removes some unpleasant hassles:
Polling Database with Timer[^].

—SA
 
Share this answer
 
Comments
P.S Vijay 27-Feb-13 1:17am    
Thanks
Sergey Alexandrovich Kryukov 27-Feb-13 1:21am    
My pleasure.
Good luck, call again.
—SA
If you just use Google to search for the error message there are hundreds of pages showing you various tips to get around the issue.

Here[^]
 
Share this answer
 
try this

C#
if(EmployeesDataGrid.InvokeRequired)
{
    EmployeesDataGrid.Invoke(new MethodInvoker(delegate{EmployeesDataGrid.DataContext = employeesView;}));
}
else
{
   EmployeesDataGrid.DataContext = employeesView;
}
 
Share this answer
 
Comments
P.S Vijay 27-Feb-13 1:17am    
thanks

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