从外部线程使用BeginInvoke调用Action

本文关键字:调用 Action BeginInvoke 线程 从外部 | 更新日期: 2023-09-27 17:58:52

// This method never gets called
public void DoSomethingWithByte(byte b) 
{
    Writeline(b);
}
class Test<T>
{
    public Test(Action<T> act, T data)
    {
        Dispatcher.Current.BeginInvoke(act, data);
    }
}
void TestAll()
{
   new Test<Byte>(DoSomethingWithByte, 6);
}

这不起作用,为什么?

它编译,到达行,但不调用方法

为什么会发生这种情况?

从外部线程使用BeginInvoke调用Action

Act没有可调用的方法,它为null。

Action<byte> act = ((byte b) DoSomethingwithByte(b));

然后有你的方法。

public void DoSomethingWithByte(byte b) {}