The following code gives "cross thread operation" exception. Just because of "form2.ResumeLayout(false)". And if this statement is commented, I can't see browser in form. I know the need of ResumeLayout(false) but is there a solution?

namespace WindowsFormsApplication1
{
public partial class Form1:  Form
{
    private System.ComponentModel.IContainer components = null;
    protected override void Dispose(bool disposing)
    { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
    private System.Windows.Forms.Button button1;

    public Form1()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        this.button1.Location = new System.Drawing.Point(64, 47);
        this.button1.Text = this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.button1);
        this.Text = this.Name = "Form1";
        this.ResumeLayout(false);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Class1 clss = new Class1();
        clss.startme();
    }
}

class Class1
{
    public void startme()
    {
        Thread thread = new Thread(new ParameterizedThreadStart(Run));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start(null);
    }
    private void Run(object j)
    {
        WebBrowser webBrowser1 = new WebBrowser();
        webBrowser1.Dock = DockStyle.Fill;
        webBrowser1.Navigate("https://dshift.sharepoint.com");

        Form form2 = new Form();
        form2.SuspendLayout();
        form2.Controls.Add(webBrowser1);
        form2.ResumeLayout(false);
        Application.OpenForms["Form1"].Invoke(new MethodInvoker(delegate
        {
            form2.ShowDialog(Application.OpenForms["Form1"]);
        }));
    }
}
}
link|improve this question
try calling your resume layout on Form_Load event of form2. – Sudhakar 12 mins ago
All UI should be shown in a single thread, called the UI thread. That's what the "cross-thread exception" is telling you. Why does this form need to run in a separate thread? – Cody Gray 12 mins ago
@Sudhakar: i tried adding "form2.Load += new EventHandler(form2_Load);" but i dont know why form2_Load is not fired; though form shows up (without browser). – Tamour Ahmad 6 mins ago
@Cody Gray: Actually I have a big application. I created this sample app so i can debug easily and post it on stackoverflow. That application has a thread that needs to show a form when needed (not always). Problem is i want to show it as dialog. because in some scenario, this form might go on back and user never knows he need to input some where. – Tamour Ahmad 4 mins ago
Hmm it will fire. where did you place ? – Sudhakar 4 mins ago
show 1 more comment
feedback

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown

Browse other questions tagged or ask your own question.