日期:2014-05-17  浏览次数:20833 次

怎样在一个form窗体里开两个UI线程
我现在有个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各位牛人帮忙想想办法。
C# code
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"