Dispatcher.Invoke((ThreadStart ... [英] Dispatcher.Invoke((ThreadStart...

查看:100
本文介绍了Dispatcher.Invoke((ThreadStart ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已经在项目中为ICollection f实现了Extension方法,从根本上来说,我正在尝试对ApplicationIdle状态执行一些与Datagrid相关的操作.这是代码.

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.

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);
       }
   }



但是在代码 Dispatcher.Invoke (((ThreadStart)delegate()

错误:
非静态字段,方法或属性需要对象引用.我做错了什么问题.



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.

推荐答案

我使用了Dispatcher,但它似乎不是很可靠.有了更多的任务运气:首先从UI线程上的某个位置获取TaskScheduler:

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:

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



希望对您有所帮助.



Hope it helps.


这篇关于Dispatcher.Invoke((ThreadStart ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆