Click here to Skip to main content
Rate this: bad
good
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);
                directoryInfo.GetDirectories();
 }
 
then I have two scenarios to try to get around the delay
 
Scenario 1:
I call it like:
 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:
 

//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 20-Jan-12 16:01pm
Comments
Nathan Stiles at 21-Jan-12 10:24am
   
You can also try System.IO.Directory.Exists

1 solution

Rate this: bad
good
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()
        {
            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();
        }
    }
}
  Permalink  
Comments
mikewhitney at 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
0 OriginalGriff 285
1 DamithSL 265
2 CPallini 195
3 Maciej Los 175
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 21 Jan 2012
Copyright © CodeProject, 1999-2014
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