All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
用10个网络规划PEAR类简化PHP编码_[PHP教程]
C#教程:多线程简介
PHPer:让我们拥抱敏捷吧_php资料_编程技术
Asp.net控件开发(九)之复合控件(3)事件机制_[Asp.Net教程]
安装SQL Server 2000的错误及解决_[SQL Server教程]
ASP.NET生成饼图功能实例代码_[Asp.Net教程]
C#,中启动进程的三种方法_[Asp.Net教程]
理解PHP5中static和const关键字_[PHP教程]
Html网页页面head区规范知识_[Html教程]
Linux操作系统中用PHP构建网站详解_php资料_编程技术
Reading number is top 10 pictures
福利福利。。。。。。
赵惟依写真2
Sora aoi on twitter4
Summer is most suitable for young people to travel in China6
The sixties of the last century, China is such a kill pig
Valentine's day comes, send some cartoon
A man's favorite things15
A man's favorite things9
迷人的靓女
天安门景物系列(一)
Download software ranking
VC++6.0培训教程
Boxer's Top ten classic battle5
Unix video tutorial14
Tram sex maniac 2 (H) rar bag11
小黑猫大战两米大花蛇
Tram sex maniac 2 (H) rar bag7
Tram sex maniac 2 (H) rar bag8
Macromedia Dreamweaver 8
Professional killers2 for Android
Tram sex maniac 2 (H) rar bag14
qq published in(发表于) 2014/7/11 9:23:32 Edit(编辑)
C#教程:删除线程

C#教程:删除线程

C#教程:删除线程

删除线程

线程的Abort方法用于永久地停止托管线程。调用Abort方法时,公共语言运行库在目标线程中引发ThreadAbortException,目标线程可捕捉此异常。一旦线程被中止,它将无法重新启动。

如果在应用程序中使用了多线程,辅助线程还没有执行完毕,在关闭窗体的时候必须要关闭辅助线程,否则会引发异常。

示例

线程的删除

下面的代码实现了删除线程t的功能。

namespace ThreadTest

{

public partial class Form1 : Form

{

private Thread t = null;

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

t = new Thread(new ThreadStart(ThMethord));

t.Start();

}

public void ThMethord()

{

MessageBox.Show("线程启动");

}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)

{

if (t.IsAlive)

{

t.Abort();//关闭线程

}

}

}

}

完整程序代码如下:

★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Threading;

using System.Diagnostics;

using System.Net.Mail;

using System.IO;

namespace ThreadTest

{

public partial class Form1 : Form

{

private Thread t = null;

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

t = new Thread(new ThreadStart(ThMethord));

t.Start();

}

public void ThMethord()

{

MessageBox.Show("线程启动!");

}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)

{

if (t.IsAlive)

{

t.Abort();

}

}

}

}

★ ★★★★Form1.Designer.cs窗体设计文件完整程序代码★★★★★

namespace ThreadTest

{

partial class Form1

{

///



/// 必需的设计器变量。

///


private System.ComponentModel.IContainer components = null;

///

/// 清理所有正在使用的资源。

///


/// 如果应释放托管资源,为 true;否则为 false。

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows 窗体设计器生成的代码

///

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

///


private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(122, 191);

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);

//

// Form1

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(345, 287);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.