none
Unable to execute a method in a new thread (multi-threading issue) RRS feed

  • Question

  •  

    Hello everyone. I'm trying to execute a long task (scanning a whole drive to find a specific file) in a new thread, in order to keep my MainWindow responsive.

     

    So basically, I have a button, which once clicked fires off a new thread to scan the whole drive. I'm hoping to be able to work on the WPF main window while the drive is being scanned. Unfortunately, that's not what is happening. My main window gets unresponsive until the drive finishes being scanned. I do get the expected result from the scan, but still, it looks like the whole job was run in the main thread.

     

    Here is some code :

    using System.Threading;
    
    using System.Threading.Tasks;
    
    private void locatorBtn_Click(object sender, RoutedEventArgs e)
            {
                string[] foundBinaries = null;
                ParallelOptions po = new ParallelOptions();
                po.MaxDegreeOfParallelism = -1;
    
                //if (!String.IsNullOrEmpty(binloc.mysqldumpClientBinary))
                //    return;
    
                Parallel.Invoke(po, () => foundBinaries = locate());
    
                if (foundBinaries != null)
                    MessageBox.Show(foundBinaries[0], "RESULT");
            }
    
            private string[] locate()
            {
                string[] foundBinaries =  FileUtils.FindFile(SourceRechercheEnum.AllDrives, "mysqldump.exe", false));
    
                MessageBox.Show("Thread #" + Thread.CurrentThread.ManagedThreadId.ToString(),"Leaving thread");
                return foundBinaries;
            }
    

    The MessageBox.Show() methods in locate() do confirm I enter in the new thread. FileUtils.FindFile() is a method in a type library I've written awhile back. It basically performs a straightforward (recursive) GetFiles().

    So OK, it looks like I do enter in a new thread, but the app is still unresponsive (not "barely responsive".. totally unresponsive, as if Parallel.Invoke() was called synchronously.

    Any idea on what I'm not getting, what I'm doing wrong ?

    Many thanks,

    Jeff

     (edit: rewrote locate() .. same result as described below)
    • Edited by Jeff Gratton Thursday, September 22, 2011 2:28 AM
    Thursday, September 22, 2011 2:24 AM

Answers

  • Your use of the Parallel class, which is new to me, is incorrect as per MSDN Online.  Or not incorrect, but it doesn't work as you expect it to work.  It reads:  "No guarantees are made about the order in which the operations execute or whether they execute in parallel. This method does not return until each of the provided operations has completed, regardless of whether completion occurs due to normal or exceptional termination."

    So as you can see, Parallel.Invoke() is in itself a blocking call.  Solution?  Call Parallel.Invoke() from a new thread.  Good ol' Thread class.


    MCP
    • Proposed as answer by Paul Zhou Friday, September 23, 2011 8:19 AM
    • Marked as answer by Jeff Gratton Friday, September 23, 2011 2:43 PM
    Thursday, September 22, 2011 5:44 AM

All replies

  • Your use of the Parallel class, which is new to me, is incorrect as per MSDN Online.  Or not incorrect, but it doesn't work as you expect it to work.  It reads:  "No guarantees are made about the order in which the operations execute or whether they execute in parallel. This method does not return until each of the provided operations has completed, regardless of whether completion occurs due to normal or exceptional termination."

    So as you can see, Parallel.Invoke() is in itself a blocking call.  Solution?  Call Parallel.Invoke() from a new thread.  Good ol' Thread class.


    MCP
    • Proposed as answer by Paul Zhou Friday, September 23, 2011 8:19 AM
    • Marked as answer by Jeff Gratton Friday, September 23, 2011 2:43 PM
    Thursday, September 22, 2011 5:44 AM
  • Yes, I stumbled upon your hilighted sentence recently.

     

    I've drop the Parallel class from my code and reverted to good old thread-handling, everything works fine now.

     

    Thanks again, webJose !

     

    -- Jeff

    Friday, September 23, 2011 2:45 PM