1

how do i let UIthread to execute a command when im in some thread the code below is called by a thread but the line i want to run in UIthread .. doesn't work if i called it like that ..
the form lag a little and the process fan got fast like it's in infinity loop.. then it gives me an error "stackoverflowexception"

my application is a file manager .. (copy, cut, paste, new folder..etc) .. and dirRecursive(string path) .. shows me the files and folders in a listView with its icons so every time i do something like (new folder or paste) i have to call dirRecursive to update the listView

note:

  • it works excellent before i tried to execute PasteFromCopy with a thread ..
  • it works excellent when i remove the dirRecursive(..) line from the paste method .. but i need to update listview automatically after the paste is done .. that why i have to call it from PasteFromCopy but using UIThread
  • if i used UIThread to PASTE then the form will lag when a file is being copyied .. you know

    please help :) thanks in advance

    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
            }
        }
    
  • M.Babcock
    • 18,753
    • 6
    • 54
    • 84
    Murhaf Sousli
    • 12,622
    • 20
    • 119
    • 185
    • possible duplicate of [How to update GUI from another thread in C#?](http://stackoverflow.com/questions/661561/how-to-update-gui-from-another-thread-in-c) – M.Babcock Feb 08 '12 at 01:28
    • 1
      Please do not prefix your question titles with c# or the like; that is what the tags are for. – M.Babcock Feb 08 '12 at 01:28
    • the title of the link that you just gave me had C# .. !! and this's to update a property of a control .. not to call a function that i have to run it in UIThread.. please read my question before you answer – Murhaf Sousli Feb 08 '12 at 01:40
    • i need a way to call a simple function in UI thread.. how do i do that dispatcher ? – Murhaf Sousli Feb 08 '12 at 01:44

    1 Answers1

    2

    try replacing this line

    dirRecursive(currAddress);
    

    with

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

    This is assuming you're using WinForms and not WPF, you haven't specified. Also 'InvokeRequired' and 'Invoke' are both members of a System.Windows.Forms.Control so your PasteFromCopy would need to be a method on your form.

    SynXsiS
    • 1,860
    • 10
    • 12