Question

Ok, this is the main essence of what I am trying to achieve and the symptoms of what it is doing.

I have a main windows form. On this form the user can click on a button that will open up a new and seperate form. This form will have a button that is supposed to display a FolderBrowserDialog. Instead it simply locks up form2 and never displays anything.

Here is essentially the code I have dealing with the form etc.

This is in the first form that is loaded after i do some uninteresting things.

FORM1.cs
//do stuff
//In a button.click method I do the following
Application.Run(new Form2(myParameters1, 2, 3));

This is the second form that is called from the first form

FORM2.cs
//do more stuff with the parameters on load
//user clicks on a button
private void button1_Click(object sender, EventArgs e)
{
    FolderBrowserDialog fbd = new FolderBrowserDialog();
    fbd.RootFolder = Environment.SpecialFolder.Desktop;
    fbd.Description = "This is the browser dialog box";

    if(fbd.ShowDialog() == DialogResult.OK)
    {
        //do stuff
    }
}

After I click on the button the dialog box does not show up and the form2 gets locked from doing anything.

I have also attempted changing the

fbd.ShowDialog() 

To

fbd.ShowDialog(Form2.ActiveForm)

with the same results.

Any help would be appreciated! If you need more info let me know and I can try to provide all that I can.

EDIT

I forgot to mention (and actually completely forgot) That the method that opens up the second form is a seperate thread.

So the first form starts a thread, which opens the second form, which is supposed to open a dialog which it is not. Now i think it has to do with the threading..

Was it helpful?

Solution

I have figured out my issue. It ended up being that the thread from Form1 that was opening Form2 was not able to open DialogBoxes because it was seperate from the UI thread entirely.

I ended up working around using that thread and just eliminated it completely which solved my problem. The dialog box opened up just as I wanted.

Thank you all for the responses though! They did help me figure out a few other things I failed to do correctly.

OTHER TIPS

I had similar problem. Main GUI thread was creating a backgroundworker thread to write to database but when background thread is failed used to show a custom control dialog to save exception file. THis custom dialog was shown correctly however Browse button on it to open folderBrowserDialog to save the exception file wouldn't show. My custom control would show "Not Responding" in its title bar.

What I did was instead of calling Custom Control directly I made it call on UI thread itself like this.

void DBThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {                
            this.Invoke(new CrossThreadExceptionHandler(CatchInterThreadException), e.Error);                
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top