Avatar of laitt
laitt
 asked on

Start second Form with WebBrowser control in a new thread

I have 2 forms, Form1 is the main form, the second is IEForm with a WebBrowser control.

Pls check the code below, when I run the project I got this error message: "Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang."

I tried to turn off LoaderLock check in Debug / Exceptions, the LoaderLock error then disappeared but IEForm now doesnt display.

I'm so confused, pls help me to resolve this issue.

Thank you very much.
namespace Test_Thread
{
    public partial class Form1 : Form
    {
        public Thread ieThread;
        public IEForm xForm;
 
       ...........................................
 
        public void ThreadTask()
        {
            xForm = new IEForm();
            xForm.Show();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            ieThread = new Thread(new ThreadStart(this.ThreadTask));
            ieThread.SetApartmentState(ApartmentState.STA);
            ieThread.IsBackground = false;
            ieThread.Start();
        }
    }
}

Open in new window

C#

Avatar of undefined
Last Comment
jask24

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jask24

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes