Link to home
Start Free TrialLog in
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

ASKER CERTIFIED SOLUTION
Avatar of jask24
jask24

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial