0

my code is--

    protected void BrowseButtonClick(object sender, EventArgs e)
    { 
        Thread newThread = new Thread(new ThreadStart(ThreadMethod));
        newThread.SetApartmentState(ApartmentState.STA);
        newThread.Start();
    }


    void ThreadMethod()
    {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.ShowDialog();
            BrowseTextBox.Text = string.Format("{0}/{1}", Path.GetDirectoryName(dlg.FileName), dlg.FileName);
    }

please help......

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
rajnivk
  • 11
  • 1
  • 3
  • 3
    You may find you need to `Invoke` the call to the `Text` property. See: http://stackoverflow.com/questions/782274/using-c-sharp-methodinvoker-invoke-for-a-gui-app-is-this-good – rhughes Mar 20 '13 at 12:06
  • i am unable to write the path of the browsed file into the browsetextbox – rajnivk Mar 20 '13 at 12:47
  • the code has no prblm except what is stated above – rajnivk Mar 20 '13 at 13:02

2 Answers2

0

It's not very clear what you really need. I assume that you have a problem with updating a UI control from another thread.

void ThreadMethod()
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.ShowDialog();
    MethodInvoker invoker = delegate 
    {
        BrowseTextBox.Text = .... 
    };

    if(InvokeRequired)
    {
        Invoke(invoker);
    }
    else
    {
        invoker();
    }
}

UPDATE

For WPF application you should change the code above. Here is the example:

Action invoker = delegate 
    {
        BrowseTextBox.Text = .... 
    };
Dispatcher.Invoke(invoker);
Ronnix
  • 310
  • 4
  • 12
  • my code is creating a browse button and i used a thread to handle sta exception but i am unable to write the path of the browsed file into the browsetextbox – rajnivk Mar 20 '13 at 12:46
  • You must always update UI controls from the GUI thread. I gave you an example how to do this for WinForm application. If you are building a WPF application, the updating controls will be slightly different. – Ronnix Mar 20 '13 at 13:48
-1

BrowseTextBox.Text = dlg.FileName

Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83
  • 1
    I don't think this is the answer. The OP's code related to doing this bit seems to be fine. I suspect it is a threading issue – musefan Mar 20 '13 at 12:07