FolderBrowserDiaog in Batch Task not working

Hello,

I want to open a FolderBrowserDiaog when a Batch Task is initialized. The problem, the Dialog does not appear. It appears to be invisibel.

I think I have to set the parent form. How to I get access to it? Or is there a better solution?

Thanks

Parents Reply
  • Hi Andre,

    I see the problem now.

    Tasks are run on separate threads and the FolderBrowserDialog is special (a shell dialog), which is why you cannot see the dialog.

    See for more details.

    Try adding the following code to the OnIntializeTask method:

    DialogResult r = DialogResult.No;
    FolderBrowserDialog dialog = new FolderBrowserDialog();
    var t = new Thread(() => r = dialog.ShowDialog());
    t.IsBackground = true;
    t.SetApartmentState(ApartmentState.STA);
    t.Start();
    t.Join();
    
    if (r == DialogResult.OK)
    {
        var folder = dialog.SelectedPath;
    }
Children