我在使用以下代码时收到以下错误:
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(
     new Action(() => actualServer.SetServerData(serverData)));


here中查找Invoke重载的完整列表

10-04 12:31
查看更多