Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
 private void DirCheck( string startPath) {
var directoryInfo = new DirectoryInfo(startPath);
                directoryInfo.GetDirectories();
 }


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

Scenario 1:
I call it like:
C#
Task Test = Task.Factory.StartNew(() =>
    DirCheck("\\netstuff\shared");


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


Scenario 2:


C#
//BackGroundWorker1
   private void TreeWorker(object sender, DoWorkEventArgs e)
   {
       string strVal;
       strVal = (string)e.Argument;

       DirCheck(strVal);
   }


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?



Thanks.
Posted
Comments
Nathan Stiles 21-Jan-12 10:24am    
You can also try System.IO.Directory.Exists

1 solution

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

C#
using System;
using System.Windows.Forms;
using System.Threading;

namespace ReflectRefCount
{
    public partial class Form1 : Form
    {
        private delegate void dVoidObject(object anyType);
        public Form1()
        {
            InitializeComponent();
        }

        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);
            }
            else
            {
                if (anyType != null)
                {
                    Text = anyType.ToString();
                }
            }
        }

        private void TaskThatTakesALongTime(object aNumber)
        {
            int i = (int)aNumber;
            Thread.Sleep(i);
            TaskToUpdateTheUserInterface("The task is complete");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StartLongTaskWithoutUIHang();
        }
    }
}
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900