0

I have two projects within my solution and in each there is one form.

I am trying to open the second form within an if statement but what happens is, when the new form is declared, the code suddenly breaks at that point and moves onto the client.close(); without completing the declaration.

I usually program in asp.net and I haven't seen something like this before, the code never runs through to .ShowDialog() and doesn't break with error.

  else if (DeviceRead == "DeviceRead:2")
  {
      _RequestID = RequestID.Remove(0, 10);
      _DeviceID = DeviceRead.Remove(0, 11);

      //this.Hide();

      GenericEPadDemo.frmMain sig = new GenericEPadDemo.frmMain();
      sig.ShowDialog();

  }  

 }
 s.Close();

 }
 finally
 {
    client.Close();
 }

The error being returned is: ActiveX control '30778fc6-eaba-43a7-ba39-6875a3b16057' cannot be instantiated because the current thread is not in a single-threaded apartment.

Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
connersz
  • 1,153
  • 3
  • 23
  • 64

1 Answers1

1

Quote:

" Go ahead and add [STAThread] to the main entry of your application, this indicates the COM threading model is single-threaded apartment (STA)

example:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new WebBrowser());
    }
}

"

Check the second answer here: Single-threaded apartment - cannot instantiate ActiveX control

[later edit] try the first answer in the link above, like so:

Thread t = new Thread(new ThreadStart(() =>
{
    {
        GenericEPadDemo.frmMain sig = new GenericEPadDemo.frmMain();
        sig.ShowDialog();
    };
}));
t.SetApartmentState(ApartmentState.STA);
t.Start();
Community
  • 1
  • 1
Adrian Nasui
  • 1,054
  • 9
  • 10