Dispatcher.CurrentDispatcher.Invoke

本文关键字:Invoke CurrentDispatcher Dispatcher | 更新日期: 2023-09-27 18:36:08

使用以下代码时收到以下错误:Dispatcher.CurrentDispatcher.Invoke(() => actualServer.SetServerData(serverData));无法将 lambda 表达式转换为类型"System.Delegate",因为它不是委托类型

public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        var source = new List<ServerProperty>
        {
            new ServerProperty("connectionstring1", "server1"),
            new ServerProperty("connectionstring2", "server2"),
            new ServerProperty("connectionstring3", "server3"),
            new ServerProperty("connectionstring4", "server4"),
        };
        DataContext = source;
        _timer = new Timer((t) =>
        {
            foreach (var serverProperty in source)
            {
                ServerProperty server = serverProperty;
                ThreadPool.QueueUserWorkItem((o) =>
                {
                    var serverData = ServerDataCalculator.GetServerData(server.ConnectionString);
                    var actualServer = (ServerProperty)o;
                    Dispatcher.CurrentDispatcher.Invoke(() => actualServer.SetServerData(serverData));
                }, server);
            }
        }, null, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
    }

Dispatcher.CurrentDispatcher.Invoke

没有超载接受委托。尝试:

Dispatcher.CurrentDispatcher.Invoke(
     new Action(() => actualServer.SetServerData(serverData)));

在此处查看Invoke重载的完整列表