我现在有个form有两个控件,一个是WebBrowser控件,一个是普通按钮控件A。我现在要做个调试网页功能,通过网页里window.external.Wait方法调用本地方法Wait,然后挂起网页,比如当网页运行到window.external.Wait方法时就把网页挂起来不让它继续执行。同时form里的普通按钮控件A可以操作恢复和挂起网页。 现在的问题是当我挂起网页时整个form窗体就处于假死状态。我想到的办法是开两个UI线程,一个UI线程A处理WebBrowser,另一个UI线程B处理form,当UI线程A挂起时,UI线程B可继续运行,并可挂起和恢复UI线程A。 但是现在Winform的机制好像是一个应用程序只能有一个UI线程,有没有其他办法解决这个问题,这问题困扰我好几天了,所有求助csdn各位牛人帮忙想想办法。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication8
{
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public  class Form1 : Form
    {
        private System.Windows.Forms.WebBrowser webBrowser1;
        private System.Windows.Forms.Button button1;
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;        public Form1()
        {
            InitializeComponent();
            webBrowser1.ObjectForScripting = this;
            webBrowser1.DocumentText = "<html><head><title></title> " +
                "<script type=\"text/javascript\"> window.external.Wait();" +
           " alert('test!'); </script> </head> <body> </body></html>";
            
            }
        protected override void OnLoad(EventArgs e)
        {
         
          
        }        private void button1_Click(object sender, EventArgs e)
        {        }
        public void Wait() 
        {
//挂起后怎么才能让窗体不处于假死状态
            System.Threading.Thread.CurrentThread.Suspend();
        }
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }        #region Windows 窗体设计器生成的代码        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.webBrowser1 = new System.Windows.Forms.WebBrowser();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // webBrowser1
            // 
            this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Top;
            this.webBrowser1.Location = new System.Drawing.Point(0, 0);
            this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
            this.webBrowser1.Name = "webBrowser1";
            this.webBrowser1.Size = new System.Drawing.Size(718, 204);
            this.webBrowser1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(158, 224);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "挂起恢复";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(718, 283);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.webBrowser1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);        }        #endregion
    }    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }}

解决方案 »

  1.   

    添加个允许跨线程访问控件,把要操作的代码块放在新线程里面试试。
    CheckForIllegalCrossThreadCalls = false;
      

  2.   


    我的目的不是跨线程访问控件,而是挂起webBrowser控件时,界面的上的其他控件可以继续操作。
      

  3.   

    关键在于我怎样才能开出两个线程来,比如一个form里有A控件和B控件,我怎样把它们分别放在不同的线程里。
      

  4.   


            private void button1_Click(object sender, EventArgs e)
            {
                Thread t = new Thread(Test);
                t.Start();
            }        private void button2_Click(object sender, EventArgs e)
            {
                Thread t = new Thread(Test);
                t.Start();
            }        private void Test()
            {
                Thread.Sleep(3000);
            }
    比如说象这样,点了button1后,界面并不会卡起来。
    button2一样可以点击。
      

  5.   


    你是开了一个后台线程,处理后台的任务,我的是前台线程也就是UI线程,你这个无法处理我那个问题,你可以把我上面的代码 拿到vs里跑一下就知道了,我的目的很简单,当js调用window.external.Wait();方法时挂起来,不继续执行下面的alert方法,同时界面上按钮要保持可用
      

  6.   


            private void DoWait()
            {
                Thread t = new Thread(Wait);
                t.Start();
            }        public void Wait() 
            {
                //挂起后怎么才能让窗体不处于假死状态
                System.Threading.Thread.CurrentThread.Suspend();
            }
            //window.external.DoWait();
      

  7.   


    这种方法不行,你这种方法当window.external.Wait();执行完后,会继续执行alert方法。
      

  8.   

    能否换个思路?把web控件放到另一个窗体上,然后用另一个线程启动这个窗体,来控制?namespace WindowsFormsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.webBrowser1 = new System.Windows.Forms.WebBrowser();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(12, 12);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "假死";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // webBrowser1
                // 
                this.webBrowser1.Location = new System.Drawing.Point(93, 12);
                this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
                this.webBrowser1.Name = "webBrowser1";
                this.webBrowser1.Size = new System.Drawing.Size(234, 306);
                this.webBrowser1.TabIndex = 1;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(494, 330);
                this.Controls.Add(this.webBrowser1);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Button button1;
            private System.Windows.Forms.WebBrowser webBrowser1;
        }
    }-------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        [System.Runtime.InteropServices.ComVisibleAttribute(true)]
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                CheckForIllegalCrossThreadCalls = false;
            }        public void Wait()
            {
                //挂起后怎么才能让窗体不处于假死状态
                System.Threading.Thread.CurrentThread.Suspend();
                //MessageBox.Show("asd");
            }
            public void setweb(int num)
            {
                this.webBrowser1.Width += num;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Wait();
            }
        }
    }--------------------------------namespace WindowsFormsApplication1
    {
        partial class Form2
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.button2 = new System.Windows.Forms.Button();
                this.button3 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(12, 12);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(111, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "异线程弹出Form1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(13, 42);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(110, 23);
                this.button2.TabIndex = 1;
                this.button2.Text = "form1取消挂起状态";
                this.button2.UseVisualStyleBackColor = true;
                this.button2.Click += new System.EventHandler(this.button2_Click);
                // 
                // button3
                // 
                this.button3.Location = new System.Drawing.Point(13, 72);
                this.button3.Name = "button3";
                this.button3.Size = new System.Drawing.Size(75, 23);
                this.button3.TabIndex = 2;
                this.button3.Text = "操作";
                this.button3.UseVisualStyleBackColor = true;
                this.button3.Click += new System.EventHandler(this.button3_Click);
                // 
                // Form2
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(136, 103);
                this.Controls.Add(this.button3);
                this.Controls.Add(this.button2);
                this.Controls.Add(this.button1);
                this.Name = "Form2";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Text = "Form2";
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Button button2;
            private System.Windows.Forms.Button button3;
        }
    }------------------using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            System.Threading.Thread th;
            private void button1_Click(object sender, EventArgs e)
            {
                th = new System.Threading.Thread(ShowF1);
                th.SetApartmentState(System.Threading.ApartmentState.STA);//这句是关键
                th.Start();
            }
            Form1 f1;
            private void ShowF1()
            {
               f1 = new Form1();
                f1.ShowDialog();
            }
        }
    }
    -----------------------------------------using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form2());
            }
        }
    }
      

  9.   

    路过打酱油,以下皆为无责任google关键字——
    “WINFORM 子窗体”
    “WINFORM UI线程”
    “WINFORM 跨线程UI”PS:请无视该楼内容
      

  10.   

      LZ没搞明白吧 怎么会有2个UI呢 可能是你表达的问题吧 你的这个做法很难啊 因为webbrowser和button是ui创建的 work线程是不可能操作它们的