将BeginInvoke代码从Winforms转换为Wpf

时间:2022-10-27 19:44:45

I have found out that ISynchronizeInvoke cannot be used in WPF after having tried to convert the following code unsuccessfully. Can anybody help?

我发现在尝试转换以下代码失败后,无法在WPF中使用ISynchronizeInvoke。有人可以帮忙吗?

private static void EVENT_R(Delegate @event, object[] data)
{
    if (@event != null)
    {
        foreach (var A_C in @event.GetInvocationList())
        {
            var NewTp = (ISynchronizeInvoke)A_C.Target;
            if (NewTp != null && NewTp.InvokeRequired)
            {
                NewTp.BeginInvoke(A_C, data);
            }
            else
            {
                A_C.DynamicInvoke(data);
            }
        }
    }
}

1 个解决方案

#1


0  

If NewTp is a UIElement, then you need to cast to UIElement and call UIElement.Dispatcher.BeginInvoke:

如果NewTp是UIElement,那么您需要转换为UIElement并调用UIElement.Dispatcher.BeginInvoke:

var NewTp = (UIElement)A_C.Target;
if (NewTp != null)
{
    NewTp.Dispatcher.BeginInvoke(A_C, data);
}

#1


0  

If NewTp is a UIElement, then you need to cast to UIElement and call UIElement.Dispatcher.BeginInvoke:

如果NewTp是UIElement,那么您需要转换为UIElement并调用UIElement.Dispatcher.BeginInvoke:

var NewTp = (UIElement)A_C.Target;
if (NewTp != null)
{
    NewTp.Dispatcher.BeginInvoke(A_C, data);
}