C# 当im在某个线程中时,如何让UI线程执行命令

C# 当im在某个线程中时,如何让UI线程执行命令,c#,multithreading,ui-thread,C#,Multithreading,Ui Thread,当im在某个线程中时,如何让UIthread执行命令 下面的代码由线程调用,但我希望在UIthread中运行的行。。如果我这样叫它,那就不行了。 形式有点滞后,过程风扇变得很快,就像在无限循环中一样。。然后它给我一个错误“stackoverflowexception” 我的应用程序是一个文件管理器。。(复制、剪切、粘贴、新建文件夹等)。。和dirRecursive(字符串路径)。。显示listView中的文件和文件夹及其图标,因此每次执行类似操作(新建文件夹或粘贴)时,我都必须调用dirRecu

当im在某个线程中时,如何让UIthread执行命令 下面的代码由线程调用,但我希望在UIthread中运行的行。。如果我这样叫它,那就不行了。
形式有点滞后,过程风扇变得很快,就像在无限循环中一样。。然后它给我一个错误“stackoverflowexception”

我的应用程序是一个文件管理器。。(复制、剪切、粘贴、新建文件夹等)。。和dirRecursive(字符串路径)。。显示listView中的文件和文件夹及其图标,因此每次执行类似操作(新建文件夹或粘贴)时,我都必须调用dirRecursive来更新listView

注意:
  • 在我尝试使用线程执行PasteFromCopy之前,它工作得非常好。。
  • 当我从粘贴方法中删除dirRecursive(..)行时,效果非常好。。但是我需要在粘贴完成后自动更新listview。。这就是为什么我必须从PasteFromCopy调用它,但使用UIThread
  • 如果我使用UIThread进行粘贴,那么在复制文件时表单将延迟。。你知道吗

    请帮忙:)提前谢谢

    private void PasteFromCopy(object dest)
        {
            foreach (ListViewItem item in copiedItems)
            {
                string _dest = (string)dest;
                string itemName = item.Text;
                string itemPath = item.ToolTipText;
                string itemDest = Path.Combine(_dest, itemName);
                if (IsFolder(itemPath))
                {
                    if (Directory.Exists(itemDest))
                    {
                        if (MessageBox.Show(itemName + " is already exists .. Do you want to overwrite it and its all contents?"
                            , "Overwrite", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
                        {
                            CopyDirectory(itemPath, itemDest, true);
                        }
                    }
                    else
                        CopyDirectory(itemPath, itemDest, false);
                }
                else
                {
                    if (File.Exists(itemDest))
                    {
                        if (MessageBox.Show(itemName + " is already exists .. Do you want to overwrite it?"
                        , "Overwrite", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
                        {
                            InfoLabel("Copying " + itemName + " ...");
                            File.Copy(itemPath, itemDest, true);
                        }
                    }
                    else
                    {
                        InfoLabel("Copying " + itemName + " ...");
                        File.Copy(itemPath, itemDest, false);
                    }
                }
                InfoLabel("Paste done.");
    
                dirRecursive(currAddress);   // here is line i need to execute from UIthread
            }
        }
    

    试着换掉这条线

    dirRecursive(currAddress);
    


    这是假设您使用的是WinForms而不是WPF,您没有指定。另外,“InvokeRequired”和“Invoke”都是System.Windows.Forms.Control的成员,因此您的PasteFromCopy需要成为表单上的一个方法。

    可能的重复项请不要在问题标题前加c#或类似的前缀;这就是标签的用途。你刚才给我的链接标题是C#!!这是为了更新控件的属性。。不调用必须在UIThread中运行的函数。。在回答之前,请先阅读我的问题。我需要一种在UI线程中调用简单函数的方法。。我该怎么做?
    if (InvokeRequired)
    {
        Action a = ()=>dirRecursive(currAddress);
        Invoke(a);
    }