2

I am working on SAP B1 Add-On using C#. When I call OpenFileDialog.ShowDialog() this exception is thrown:

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. I added [STAThread] before the Main method but still have the same problem.

user3114639
  • 1,895
  • 16
  • 42
Montaser
  • 57
  • 5

1 Answers1

2

I Found the solution i added a new thread and it works fine, Here is the code

Thread t = new Thread(() =>
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();


            DialogResult dr = openFileDialog.ShowDialog(new Form());
            if (dr == DialogResult.OK)
            {
                string fileName = openFileDialog.FileName;
                this.EditText0.Value = fileName;
                SAPbouiCOM.Framework.Application.SBO_Application.MessageBox(fileName);
            }
        });          // Kick off a new thread
        t.IsBackground = true;
        t.SetApartmentState(ApartmentState.STA);
        t.Start();     
Montaser
  • 57
  • 5