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:
On the machine where I get the problem the BackgroundWorker does not run because the DoWork event is never fired:
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?