Some events do not fire after displaying a FolderbrowserDialog

I have very strange behavior when using FolderBrowserDialog in a C # application. After the FolderBrowserDialog is displayed, some events will not be fired in the application, for example, the DoWork event for BackgroundWorker or the renamed FileSystemWatcher event.

Please note that this problem only occurs on certain machines. The problem does not occur on my development machine, nor on many other production machines with the same equipment (Xeon W3550 processor) as the machines where the problem occurs. All machines running the application 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 2 snapshots below after displaying the FolderBrowserDialog and I click OK or Cancel in the form of the FolderBrowserDialog.

On my dev PC, I get the following:

Normal behavior

On the machine where the problem occurs, BackgroundWorker does not fire because the DoWork event never fires:

Problemm

If the following line is deleted in the code, the worker works as expected on all machines.

 folderbrowserDialog.ShowDialog(); 

Additional Information

  • If I replaced FolderbrowserDialog with OpenFileDialog, the worker will work as expected. So it really looks like a FolderbrowserDialog component ...
  • A few weeks ago, I tried installing Visual Studio on a machine where I was having a problem. The application works as expected only when launched from the debugger. The problem occurred when starting without debugging.
  • Switching to .NET FW 3.0 and 4.0 did not solve the problem.
  • A user logged on to Windows has administrator privileges.
  • Replacing the PC did not solve the problem.

Do you have ideas to explain how this might cause FolderbrowserDialog?

+4
source share
1 answer

FolderDialogBrowser.ShowDialog is a blocking call. You need to run it in a separate thread, as suggested in this SO post . Do you expect this to be a blocking call (i.e., it should pass data to the second employee)?

+2
source

Source: https://habr.com/ru/post/1395627/


All Articles