Dispatch.Invoke(新动作......)与参数参数、新动作、Dispatch、Invoke

2023-09-04 02:39:55 作者:明知是戏

previously我有

  Dispatcher.Invoke(新动作(()=> colorManager.Update()));
 

从另一个线程更新显示为WPF。由于设计,我不得不改变计划,我必须通过ColorImageFrame参数为我ColorStreamManager.Update()方法。

随着这个的链接,我修改了调度员:

  Dispatcher.Invoke(新动作< ColorStreamManager,ColorImageFrame>((P,V)=> p.Update(V)));
 

它编译罚款,但不会跑的。 VS2010说参数计数不匹​​配。在我的 Col​​orStreamManager.Update()方法,我有 RaisePropertyChanged(()=>位图);

有人能指出我上哪儿去了?

Col​​orStreamManager.Update()方法的签名如下:

 公共无效更新(ColorImageFrame帧);
 
IDispatch接口 GetIDsOfNames和Invoke 零度香的博客 CSDN博客 idispatch invoke 0x80020009

解决方案

您不想要的操作,有参数,因为调度是不会知道什么要传递给方法。相反,你可以做的就是关闭了变量:

  Col​​orImageFrame someFrame = ...;
Dispatcher.Invoke(新动作(()=> colorManager.Update(someFrame)));
 

Previously I had

Dispatcher.Invoke(new Action(() => colorManager.Update()));

to update display to WPF from another thread. Due to design, I had to alter the program, and I must pass ColorImageFrame parameter into my ColorStreamManager.Update() method.

Following this link, I modified my dispatcher to:

Dispatcher.Invoke(new Action<ColorStreamManager, ColorImageFrame>((p,v) => p.Update(v)));

It compiles fine but would not run at all. VS2010 says "Parameter count mismatch." In my ColorStreamManager.Update() method I have RaisePropertyChanged(() => Bitmap);

Could someone point out where did I go wrong?

The signature of ColorStreamManager.Update() method is the following:

 public void Update(ColorImageFrame frame);

解决方案

You don't want the action to have parameters, because the Dispatcher isn't going to know what to pass to the method. Instead what you can do is close over the variable:

ColorImageFrame someFrame = ...;
Dispatcher.Invoke(new Action(() => colorManager.Update(someFrame)));