【问题标题】:How do invoke AddPath()?如何调用 AddPath()?
【发布时间】:2012-06-28 10:33:45
【问题描述】:

我有以下功能:

 public void AddPath(String full_path)
    {
        TreeView tree_view = TheTreeView;
        String[] split_path;
        TreeNodeCollection current_nodes;

        if (tree_view == null)
            return;
        if (String.IsNullOrEmpty(full_path))
            return;

        split_path = full_path.Split(tree_view.PathSeparator.ToCharArray());
        current_nodes = tree_view.Nodes;

        for (Int32 i = 0; i < split_path.Length; i++)
        {
            TreeNode[] found_nodes = current_nodes.Find(split_path[i], false);

            if (found_nodes.Length > 0)
            {
                current_nodes = found_nodes.First().Nodes;
            }
            else
            {
                TreeNode node;

                node = new TreeNode();
                node.Name = split_path[i]; // name is the same thing as key
                node.Text = split_path[i];

                current_nodes.Add(node);
                current_nodes = node.Nodes;
            }
        }
    }

我需要从一个单独的线程中调用这个函数。我该怎么做? 我知道如何调用 TreeView.Nodes.Add() 但我该怎么做呢? 0.o

-斯文

【问题讨论】:

标签: c# multithreading function invoke


【解决方案1】:

如果您需要从与创建它的线程不同的线程调用 UI 中的某个对象(即您的情况),答案是您实际上不能。更好的答案是您可以通过调用 winform Control.Invoke 和 WPF Dispatcher.Invoke。只有在 winform 中,您才能使用this method 调查是否确实需要调用调用。一般来说,您应该将异步部分与更新 UI 的部分分开,以避免过多的代码混乱。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-27
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多