首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 多线程 画面假死有关问题

2012-08-27 
C# 多线程 画面假死问题子线程开始,主画面假死,使用Application.DoEvents()试了试,但是子线程无法终止,能

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";            this.button1.UseVisualStyleBackColor = true;            this.button1.Click += new System.EventHandler(this.button1_Click);            //             // button2            //             this.button2.Location = new System.Drawing.Point(169, 487);            this.button2.Name = "button2";            this.button2.Size = new System.Drawing.Size(75, 23);            this.button2.TabIndex = 1;            this.button2.Text = "Cancel";            this.button2.UseVisualStyleBackColor = true;            this.button2.Click += new System.EventHandler(this.button2_Click);            //             // Form1            //             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;            this.ClientSize = new System.Drawing.Size(643, 533);            this.Controls.Add(this.button2);            this.Controls.Add(this.button1);            this.Controls.Add(this.richTextBox1);            this.Name = "Form1";            this.Text = "Form1";            this.Load += new System.EventHandler(this.Form1_Load);            this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.RichTextBox richTextBox1;        private System.Windows.Forms.Button button1;        private System.Windows.Forms.Button button2;    }} 


环境:VS2005

[解决办法]
给你一个跨线程访问的例子:http://topic.csdn.net/u/20120805/01/58230777-2767-4337-a8f8-a3725e7db4b1.html

热点排行