4

I have a very weird behavior when using a FolderBrowserDialog in a C# application. After showing the FolderBrowserDialog, some events will not fire in the application, for example the DoWork event of a BackgroundWorker or the Renamed event of a FileSystemWatcher.

Note that this problem only occurs on specific machines. The problem does not occur on my development machine, nor on many other production machines having the same hardware (Xeon W3550 CPU) that the machines where the problem occurs. All the machines where the application runs are under Windows XP SP3.

I managed to isolate the problem in a very simple application. The code is shown below:

public partial class Form1 : Form
{
    BackgroundWorker backgroundWorker;

    public Form1()
    {
        InitializeComponent();

        listBox1.Items.Add("Initialization");

        backgroundWorker = new BackgroundWorker();
        backgroundWorker.WorkerReportsProgress = true;
        backgroundWorker.WorkerSupportsCancellation = true;
        backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
        backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
        backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        listBox1.Items.Add("Running worker");

        FolderBrowserDialog folderbrowserDialog = new FolderBrowserDialog();
        folderbrowserDialog.ShowDialog(); // If this line is removed, the worker runs as expected

        backgroundWorker.RunWorkerAsync();
    }

    void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i <= 5; i++)
        {
            backgroundWorker.ReportProgress(i * 20);
            Thread.Sleep(1000);
        }
    }

    void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
    {
        listBox1.Items.Add("Worker completed");
    }

    void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        listBox1.Items.Add("Task progress: " + e.ProgressPercentage.ToString());
    }

I run the application and I get the 2 captures below after the FolderBrowserDialog is shown and I click OK or Cancel on the FolderBrowserDialog form.

On my dev PC I get this:

Normal behavior

On the machine where I get the problem the BackgroundWorker does not run because the DoWork event is never fired:

Problem

If the following line is removed in code, the worker runs as expected on all machines.

folderbrowserDialog.ShowDialog();

Additional information

  • If I replace the FolderbrowserDialog with an OpenFileDialog, the worker runs as expected. So it really seems to be linked to the FolderbrowserDialog component...
  • Some weeks ago, I tried to install Visual Studio on the machine where I got the problem. The application ran as expected only when run from the debugger. The problem appeared when running it without debugging.
  • Migrating to .NET FW 3.0 and 4.0 did not solve the problem
  • The user logged into Windows has Administrator priviledges
  • Replacing the PC did not solve the problem

Would you have any ideas to explain how the FolderbrowserDialog could cause this?

Yahia
  • 69,653
  • 9
  • 115
  • 144
Damien
  • 106
  • 4
  • You should add another trace statement before & after the `backgroundWorker.RunWorkerAsync()` call to see if it starts and returns (*maybe displaying the result of `ShowDialog()`*). – SliverNinja - MSFT Feb 09 '12 at 16:47
  • is your application's main method attributed with [STAThread]? – Digvijay Feb 10 '12 at 12:07

1 Answers1

2

FolderDialogBrowser.ShowDialog is a blocking call. You need to run it on a separate thread as suggested in this SO post. Are you expecting it to be a blocking call (i.e. is it supposed to pass data to the background worker)?

Community
  • 1
  • 1
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • I am expecting the FolderBrowserDialog to be modal. I did not mention that I get the 2 captures above after the FolderBrowserDialog is shown and I click OK or Cancel on the FolderBrowserDialog form. – Damien Feb 09 '12 at 16:16