1

使用以下代码时出现以下错误: 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));
    }
4

1 回答 1

6

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

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

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

于 2013-10-21T10:16:59.830 回答