1


いくつかのスレッドで以下のコードがスレッドによって呼び出されたときにUIthread にコマンドを実行させるにはどうすればよいですか。そして、プロセスファンは無限ループのように高速になりました..その後、「stackoverflowexception」というエラーが表示されます

私のアプリケーションはファイルマネージャーです..(コピー、切り取り、貼り付け、新しいフォルダーなど)..およびdirRecursive(string path) ..ファイルとフォルダーをlistViewにアイコンで表示するので、次のようなことをするたびに(新しいフォルダーまたは貼り付け) listViewを更新するには、dirRecursiveを呼び出す必要があります

ノート:

  • スレッドでPasteFromCopyを実行しようとする前に、うまく機能しました..
  • 貼り付けメソッドからdirRecursive(..)行を削除するとうまく機能します..しかし、貼り付けが完了した後にリストビューを自動的に更新する必要があります..そのため、PasteFromCopy から呼び出しますが、UIThreadを使用する必要があります
  • UIThread を PASTE に使用した場合、ファイルがコピーされているときにフォームが遅れます..

    助けてください:)事前に感謝します

    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
            }
        }
    
  • 4

    1 に答える 1

    2

    この行を置き換えてみてください

    dirRecursive(currAddress);
    

    if (InvokeRequired)
    {
        Action a = ()=>dirRecursive(currAddress);
        Invoke(a);
    }
    

    これは、WPFではなくWinFormsを使用していることを前提としており、指定していません。また、「InvokeRequired」と「Invoke」はどちらもSystem.Windows.Forms.Controlのメンバーであるため、PasteFromCopyはフォームのメソッドである必要があります。

    于 2012-02-08T01:51:49.940 に答える