2

Im trying to use the FolderBrowserDialog to select a folder in C#. At first I got a Thread exception, so I googled what was wrong and fixed that but now im stuck at a nother problem. I whant to know when a folder has been selected.

This is what i'v got right now.

 private void btnWorkingFolder_Click(object sender, EventArgs e)
    {


        var t = new Thread(SelectFolder);
        t.IsBackground = true;
        t.SetApartmentState(ApartmentState.STA);
        t.Start();

    }

    private void SelectFolder()
    {
        FolderBrowserDialog dialog = new FolderBrowserDialog();
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            txtWorkFolder.Text = dialog.SelectedPath;
        }
    }
}

The problem here is that i cant Set the Text for txtWorkingFolder since im not in the same thread. I dont want to change the thread for txtWorkingFolder, so my question is this, how do I change it's value from the new thread once the DialogResult.OK has been set?

EDIT:

This is the main, btnWorkingFolder is part of the Form1():

class sample
{

    static void Main(string[] args)
    {

       Connect2Exchange conn = new Connect2Exchange();

       Application.EnableVisualStyles();
       Application.SetCompatibleTextRenderingDefault(false);

       Application.Run(new Form1());       

    }


}

SECOND EDIT:

After trying the code from examples given the following exception occurs:

System.Threading.ThreadStateException was unhandled
  Message=Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.FolderBrowserDialog.RunDialog(IntPtr hWndOwner)
       at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
       at System.Windows.Forms.CommonDialog.ShowDialog()
       at Mail2DB.Form1.btnWorkingFolder_Click(Object sender, EventArgs e) in C:\Users\marthin\documents\visual studio 2010\Projects\Mail2DB\Mail2DB\Form1.cs:line 44
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Mail2DB.sample.Main(String[] args) in C:\Users\marthin\documents\visual studio 2010\Projects\Mail2DB\Mail2DB\sample.cs:line 26
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Thx for any help! /Marthin

Marthin
  • 6,413
  • 15
  • 58
  • 95

2 Answers2

4

You should use the Invoke to delegate the execution to the GUI thread:

private void SelectFolder()
{
    FolderBrowserDialog dialog = new FolderBrowserDialog();
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        Action a = () => txtWorkFolder.Text = dialog.SelectedPath;
        this.Invoke(a);
    }
}

Also it is not very clear what you are trying to achieve here. It doesn't make any sense to use a background thread to create a file browser dialog. This task could and should very well be performed on the main thread.

Background threads are used for performing non-UI related potentially long running tasks in order to avoid blocking the main thread.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • The only reason for the new thread was the exception I got from using example at http://msdn.microsoft.com/en-us/library/bb383879(v=vs.90).aspx. So a post on Stack that gave the example for a new thread. Thx for the help though! – Marthin Jan 16 '11 at 16:25
  • @Marthin, you don't need a new thread. You could directly perform those tasks in the click callback of some button on your form. – Darin Dimitrov Jan 16 '11 at 16:26
  • @Marthin: So instead of trapping the exception you spin off another thread that will die with an unhandled exception? – Tergiver Jan 16 '11 at 16:35
  • @Dimitrov When I did this without the thread, I got a ThreadException, it was basicly the same code as above except for the thread ofc. Is there some way that i could tell the compile to ignore the exception or how do i handle it? – Marthin Jan 16 '11 at 16:41
  • @Tergiver This was ofc not my intent in the begining. Right now im trying to get a grip of this to make it the way it should be done. A full example would be greatly appreciated. Thx for all the feedback guys! – Marthin Jan 16 '11 at 16:42
4

Using a thread is very inappropriate here. The dialog is already quite capable of running on the UI thread without interfering without the updates of the rest of the windows.

The hassle with STA and getting the text box updated is only a small part of it. There's a much bigger problem, the dialog doesn't have a parent window. No other windows are available on the thread to act as the parent, only the desktop window is a candidate. The trouble starts when the user activates another window. It can overlap the dialog and there is no good way for the user to get back to it. There is no taskbar button. This can also happen by accident from an idle click at the wrong time. The user might never even see the dialog, not realizing it is actually displayed.

Another problem is that the dialog won't act modal to the rest of your windows. They stay enabled, allowing the user to operate the user interface and start the dialog again.

Just make it work like this:

private void btnWorkingFolder_Click(object sender, EventArgs e)
{
    using (var dialog = new FolderBrowserDialog()) {
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            txtWorkFolder.Text = dialog.SelectedPath;
        }
    }
}

If you really do need the dialog to run independent from the rest of your windows then you'll need to provide a 'host' window that can act as the parent. This now also requires you to pump a message loop with Application.Run(). And counter-measures against having the user bring up the dialog again, use the Enabled property.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I just tried this and got the same ThreadException: ThreadStateException was Unhandled. I edited my post with full exception. – Marthin Jan 16 '11 at 16:54
  • 2
    There's something wrong with your Main() method, normally located in Program.cs. But it seems you moved it to something resembling "Mail2DB.sample". It has to have the [STAThread] attribute, like it normally has. – Hans Passant Jan 16 '11 at 17:09
  • @Passant Thank you, that was the problem, this is a old project, dont know why iv left that out. – Marthin Jan 16 '11 at 19:58
  • I'm curious: how long has this worked without the [STAThread] attribute? Ever had strange problems with the clipboard? – Hans Passant Jan 17 '11 at 10:02
  • This is only a hobby project so it hasnt been released, but I havent had any problems with the clipboard while running in debug mode. – Marthin Jan 17 '11 at 13:49