4

以下代码给出“跨线程操作”异常。只是因为“form2.ResumeLayout(false)”。如果此语句被注释,我无法在表单中看到浏览器。我知道 ResumeLayout(false) 的需要,但有解决方案吗?

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"]);
        }));
    }
}
}
4

1 回答 1

5

WebBrowser.Navigate() 调用是问题所在。这会强制创建控件的本机窗口句柄,这发生在工作线程上。一段时间后,您强制使用 ShowDialog() 调用创建表单的本机窗口。但这发生在另一个线程上,即主 UI 线程,这要归功于 Invoke() 调用。

现在出现了不匹配,表单的窗口归主线程所有,但浏览器的窗口归工作线程所有。Winforms 提醒您这是非法的,子窗口必须与容器窗口属于同一线程。一种解决方法是将 Navigate 调用移到匿名方法中。

您可能会看到此代码,因为当您尝试在没有调用 Invoke() 的情况下显示对话框时,您也遇到了 IllegalOperationException。如果您真的想在工作线程上运行对话框,这将是正常的做法。Winforms 引发异常是因为它不希望窗口的所有者成为另一个线程上的窗口。这在 Windows 中实际上是合法的,Winforms 搞砸了检查。

您可以通过调用 SetParent() 来解决此问题。在这种非常特殊的情况下,并且永远不要在任何其他情况下这样做,方法是暂时将 Control.CheckForIllegalCrossThreadCalls 设置为 false。暂时强调一下。需要额外的工作来确保表单实际上是主线程上窗口的模态,并在对话框消失之前重新启用它:

var owner = Application.OpenForms["Form1"];
form2.Load += delegate {
    // NOTE: just as a workaround for the Owner bug!!
    Control.CheckForIllegalCrossThreadCalls = false;
    form2.Owner = owner;
    Control.CheckForIllegalCrossThreadCalls = true;
    owner.BeginInvoke(new Action(() => owner.Enabled = false));

};
form2.FormClosing += new FormClosingEventHandler((s, ea) => {
    if (!ea.Cancel) {
        owner.Invoke(new Action(() => owner.Enabled = true));
        form2.Owner = null;
    }
});
form2.ShowDialog();
于 2012-05-26T16:27:56.907 回答