views:

72

answers:

2

I'm facing problem in showing FolderBrowserDialog instance created and called from a non-UI thread. It doesn't get renders properly.

Being more specific, it doesn't shows the folder tree but displays only the Make New Folder OK and Cancel

alt text

+2  A: 

I am not sure why you would want to do this. On a worker-thread all neccessary values for your calculation should be available. There should be no need for user-interaction to get more input.
Maybe a redesign would be more helpful in your case. Think about providing the selected folder to your worker-thread before starting it.

EDIT (reply to the comment):
If you want to do some logging my answer still applies. Your worker-thread should know where to log exceptions and not start to ask the user.
Do you use a logging framework? If not, have a look at log4net for instance. Here you normally pre-configure your logging (the log-level, path, format, ...) in a xml-file. There is no user interaction needed. Though the user could change the logging path (in the xml file).

tanascius
I'm want to log exception during worker thread process and for this I need to take a path of folder from User.
Manish
That's what I don't want to do: hard-coding! I want to give user the privilege to specify a folder and log the exception there in a text file. Yes, I do use a logging framework.
Manish
@Manish: But writing it into a xml-file is not hard-coding ... it can be easily changed. You could even provide the path in your program's options ...
tanascius
Well, I am writing this program assuming the user to be non-technical and that's why I want him to select a folder to save the log file into.
Manish
@Manish: So go with the user-options dialog, where the user can change the path. However - asking the user for a logging folder during a worker-thread operation is not a good idea imho.
tanascius
+1  A: 

All the shell dialogs, including FolderBrowserDialog, require the COM apartment for the thread to be set to STA. You are probably missing the Thread.SetApartmentState() call:

    private void button1_Click(object sender, EventArgs e) {
        var t = new Thread(() => new FolderBrowserDialog().ShowDialog());
        t.IsBackground = true;
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
    }

Beware that you cannot set the owner of the dialog, it easily gets lost behind a window of another application. Which makes showing forms or dialogs on a worker thread less than a good idea.

Hans Passant
Great..can I do something like this: If an exception occurs in worker thread then 1. stop further process i.e. the thread 2. Notify the main thread somehow that an exception has occurred with having/making that Exception object available to Main thread 3. And then call the FolderBrowserDialog from Main thread. ?? If yes, plz give me some hint how this can be done.
Manish
You want to display an input dialog when an exception occurs? That's *very* unwise. By the time the user has figured out what the dialog means, she'll have completely forgotten what she did to cause the exception. Use the AppData folder to store log files.
Hans Passant