Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# System.Thread.ThreadStateException

I am trying to Initialize Web Browser Component in window form but I am getting this exception message

System.Threading.ThreadStateException: 'ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.'

I am getting above mentioned Exception Message on this line

this.browse = new System.Windows.Forms.WebBrowser();

Please tell me how to solve this issue

like image 820
Mehr Muhammad Hamza Avatar asked Sep 22 '26 21:09

Mehr Muhammad Hamza


2 Answers

Flag your Main() method with [STAThread] like below:

[STAThread]
static void Main()
{
    // your code
}

or, if the Web Browser Component should be called from inside of ant thread you created, set proper Apartment State for this thread:

yourThread.SetApartmentState(ApartmentState.STA);
like image 68
VillageTech Avatar answered Sep 25 '26 11:09

VillageTech


Add STAThread attribute above your main method.

like image 44
ShayK Avatar answered Sep 25 '26 12:09

ShayK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!