Dispatch.Invoke( new Action…) with a parameter

前端 未结 2 1564
旧巷少年郎
旧巷少年郎 2021-01-01 20:05

Previously I had

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

to update display to WPF from another thread. Due to design

相关标签:
2条回答
  • 2021-01-01 20:21

    If you call Invoke with an Action<T1, T2> delegate, you need to pass the two Action parameters to the Invoke call:

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

    The Invoke overload you're using here is Dispatcher.Invoke(Delegate, Object[]).

    0 讨论(0)
  • 2021-01-01 20:29

    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)));
    
    0 讨论(0)
提交回复
热议问题