繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> 算法/线程 >> 开心,捕捉子线程内异常的例子

开心,捕捉子线程内异常的例子

2004-10-01 08:26:10  作者:  来源:互联网  浏览次数:29  文字大小:【】【】【
简介:程序很简单,一个form内有一个按钮,点击后弹出一个对话框,点击对话框start按钮后执行新起一个线程执行一个工作类的某个方法,这个方法故意抛出异常,然后在对话框内捕捉到这个异常。 注意,这儿我是用事件来捕...
关键字:线程 例子 异常

程序很简单,一个form内有一个按钮,点击后弹出一个对话框,点击对话框start按钮后执行新起一个线程执行一个工作类的某个方法,这个方法故意抛出异常,然后在对话框内捕捉到这个异常。

注意,这儿我是用事件来捕捉异常,这只是其中一种方法

form 的代码

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace CatchThreadError

{

///

/// Form1 的摘要说明。

///

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button btnTest;

///

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

///

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码

//

}

///

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

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

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

this.SuspendLayout();

//

// btnTest

//

this.btnTest.Location = new System.Drawing.Point(192, 96);

this.btnTest.Name = "btnTest";

this.btnTest.TabIndex = 0;

this.btnTest.Text = "Test";

this.btnTest.Click += new System.EventHandler(this.btnTest_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(392, 266);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.btnTest});

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

///

/// 应用程序的主入口点。

///

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void btnTest_Click(object sender, System.EventArgs e)

{

TestDialog td = new TestDialog() ;

try

{

td.ShowDialog() ;

}

catch(Exception exp)

{

MessageBox.Show("all done") ;

}

}

}

}

dialog的代码

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Threading ;

namespace CatchThreadError

{

///

/// TestDialog 的摘要说明。

///

public class TestDialog : System.Windows.Forms.Form

{

private System.Windows.Forms.Button btnStart;

private System.Windows.Forms.Button btnClose;

///

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

///

private System.ComponentModel.Container components = null;

public TestDialog()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码

//

}

///

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

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

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

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

this.SuspendLayout();

//

// btnStart

//

this.btnStart.Location = new System.Drawing.Point(72, 32);

this.btnStart.Name = "btnStart";

this.btnStart.TabIndex = 1;

this.btnStart.Text = "Start";

this.btnStart.Click += new System.EventHandler(this.btnStart_Click);

//

// btnClose

//

this.btnClose.Location = new System.Drawing.Point(200, 32);

this.btnClose.Name = "btnClose";

this.btnClose.TabIndex = 2;

this.btnClose.Text = "Close";

this.btnClose.Click += new System.EventHandler(this.btnClose_Click);

//

// TestDialog

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(400, 94);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.btnClose,

this.btnStart});

this.Name = "TestDialog";

this.Text = "TestDialog";

this.ResumeLayout(false);

}

#endregion

private void btnClose_Click(object sender, System.EventArgs e)

{

this.Close() ;

}

private void btnStart_Click(object sender, System.EventArgs e)

{

TestClass testClass = new TestClass() ;

testClass.ThrowException += new ExceptionEventHandler(OnException) ;

Thread thread = new Thread(new ThreadStart(testClass.Run)) ;

thread.Start() ;

thread.Join() ;

}

public void OnException(ExceptionEventArgs e)

{

MessageBox.Show("程序运行出错:" + e.MyException.ToString()) ;

this.Close() ;

}

}//end class

///

/// 错误捕捉委托

///

public delegate void ExceptionEventHandler(ExceptionEventArgs e) ;

///

/// 工作类

///

public class TestClass

{

///

/// 委托

///

public event ExceptionEventHandler ThrowException ;

///

/// 委托方法

///

///

protected virtual void OnThrowException(ExceptionEventArgs e)

{

if(ThrowException != null)

{

ThrowException(e) ;

}

}

///

/// 工作方法

///

public void Run()

{

try

{

for(int i = 0 ; i < 100 ; i ++)

{

Console.WriteLine("执行到:{0}" , i) ;

Thread.Sleep(100) ;

if(i == 50)

{

throw(new Exception("error")) ;

}

}

}

catch(Exception e)

{

ExceptionEventArgs exp = new ExceptionEventArgs() ;

exp.MyException = e ;

OnThrowException(exp) ;

}

}//end method

}//end class

///

///

///

public class ExceptionEventArgs:EventArgs

{

private Exception m_objMyExcepiton ;

public Exception MyException

{

get

{

return this.m_objMyExcepiton;

}

set

{

this.m_objMyExcepiton = value ;

}

}

}//end class

}//end namespace

责任编辑:admin
相关文章