Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi all,

i have implemented a Extension method for ICollection f in project that bacically i am trying to do some datagrid related operation on ApplicationIdle state. here is the code.

C#
public static class CollectionExtension
   {
       /// <summary>
       /// Set item Binding to the DataGrid
       /// </summary>
       /// <typeparam name="T">Source type</typeparam>
       /// <param name="Collection"> Collection </param>
       public static void SetItemBinding<T>(this ICollection<T> Collection, DataGrid DataGrid)
       {
           Dispatcher.Invoke((ThreadStart)delegate()
           {
               .....

           }, DispatcherPriority.ApplicationIdle, null);
       }
   }


But am getting a error in this part of code Dispatcher.Invoke((ThreadStart)delegate()
saying that
Error:
An object reference is required for the non-static field,method or Property. what aould be the issue where am i doing wrong.
Posted
Comments
Amir Mahfoozi 26-Sep-12 11:56am    
Add a dispatcher parameter to your extension method and pass the Dispatcher instance whenever you call this extension method.

1 solution

I have used the Dispatcher, and it does not seem to be very reliable. Had much more luck with tasks: First get a TaskScheduler from somewhere that is on the UI thread:

TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();


Then in the place you need to make changes use a task:

C#
Task task = Task.Factory.StartNew(() =>
    {
       //Your code here
    }, CancellationToken.None, TaskCreationOptions.None, uiScheduler);


Hope it helps.
 
Share this answer
 
v2
Comments
[no name] 26-Sep-12 23:23pm    
Thank you very much.
Clifford Nelson 27-Sep-12 11:41am    
Glad to be of help, can you vote on the question also?

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