Click here to Skip to main content
13,006,530 members (62,427 online)
Rate this:
Please Sign up or sign in to vote.
I am writing a program that shows a drive/directory tree which then populates a files list (ListView). If the user tries to select a network drive that isn't available, the program stops responding to user input for about 25 seconds.

I think the program hangs on a call to DirectoryInfo.

so I have something like this:
 private void DirCheck( string startPath) {
var directoryInfo = new DirectoryInfo(startPath);

then I have two scenarios to try to get around the delay

Scenario 1:
I call it like:
 Task Test = Task.Factory.StartNew(() => 

On this Task call, I tried to omit any wait for the conclusion of the tasks.

Scenario 2:

   private void TreeWorker(object sender, DoWorkEventArgs e)
       string strVal;
       strVal = (string)e.Argument;

The problem is that the program still hangs for 25 seconds.

In the complete code, I am looking for an exception to occur if the drive can't be found.

The two questions then are:
1) Is there a better way to verify access to the remote drive such that there is no delay to the responsiveness of the user interface?

2) Does the GetDirectories method invariably tie-up the whole process, even if called from within a thread of the process?

Posted 20-Jan-12 15:01pm
Nathan Stiles 21-Jan-12 10:24am
You can also try System.IO.Directory.Exists

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This uses threading to perform the task off the UI thread then calls back to the UI in a thread safe way.

using System;
using System.Windows.Forms;
using System.Threading;
namespace ReflectRefCount
    public partial class Form1 : Form
        private delegate void dVoidObject(object anyType);
        public Form1()
        private void StartLongTaskWithoutUIHang()
            // here i pass 5000 (5 seconds) to TaskThatTakesALongTime this method is performed on a seperate thread 
            ThreadPool.QueueUserWorkItem(new WaitCallback(TaskThatTakesALongTime), 5000); 
        private void TaskToUpdateTheUserInterface(object anyType)
            // need to check this to prevent cross thread errors
            if (InvokeRequired)
                Invoke(new dVoidObject(TaskToUpdateTheUserInterface), anyType);
                if (anyType != null)
                    Text = anyType.ToString();
        private void TaskThatTakesALongTime(object aNumber)
            int i = (int)aNumber;
            TaskToUpdateTheUserInterface("The task is complete");
        private void button1_Click(object sender, EventArgs e)
mikewhitney 23-Jan-12 16:02pm
Thanks. It turns out I had two places that got stuck looking for the network drive. I removed the extra check and applied the technique you gave to continue the process if the network drive was found.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170628.1 | Last Updated 21 Jan 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100