日期:2014-05-18  浏览次数:20882 次

C# 多线程 画面假死问题
子线程开始,主画面假死,使用Application.DoEvents()试了试,但是子线程无法终止,能不能不用Application.DoEvents()解决画面假死的问题,代码如下,
C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;

namespace BWTest
{
    public partial class Form1 : Form
    {
        public delegate void AddListItem();
        public AddListItem myDelegate;
        private Thread my;
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// start
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            my = new Thread(new ThreadStart(Run));
            my.Start();
        }

        /// <summary>
        /// cancel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (my.ThreadState != ThreadState.Stopped)
                my.Abort();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myDelegate = new AddListItem(AddListItemMethod);
        }

        public void AddListItemMethod()
        {
            String myItem;
            for (int i = 0; i < 6000; i++)
            {
                myItem = "MyListItem"+i.ToString();
                this.richTextBox1.AppendText(myItem+"\r\n");
                this.richTextBox1.Update();
            }
        }

        public void Run()
        {
            this.richTextBox1.Invoke(myDelegate);
        }
    }
}


代码2:
C# code

namespace BWTest
{
    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.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // richTextBox1
            // 
            this.richTextBox1.Location = new System.Drawing.Point(12, 12);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(609, 446);
            this.richTextBox1.TabIndex = 0;
            this.richTextBox1.Text = "";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(37, 487);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Start";