程序很简单,一个form内有一个按钮,点击后弹出一个对话框,点击对话框start按钮后执行新起一个线程执行一个工作类的某个方法,这个方法故意抛出异常,然后在对话框内捕捉到这个异常。
注意,这儿我是用事件来捕捉异常,这只是其中一种方法
form 的代码
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace catchthreaderror
{
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.button btntest;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
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
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[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
{
/// <summary>
/// testdialog 的摘要说明。
/// </summary>
public class testdialog : system.windows.forms.form
{
private system.windows.forms.button btnstart;
private system.windows.forms.button btnclose;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public testdialog()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
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
/// <summary>
/// 错误捕捉委托
/// </summary>
public delegate void exceptioneventhandler(exceptioneventargs e) ;
/// <summary>
/// 工作类
/// </summary>
public class testclass
{
/// <summary>
/// 委托
/// </summary>
public event exceptioneventhandler throwexception ;
/// <summary>
/// 委托方法
/// </summary>
/// <param name="e"></param>
protected virtual void onthrowexception(exceptioneventargs e)
{
if(throwexception != null)
{
throwexception(e) ;
}
}
/// <summary>
/// 工作方法
/// </summary>
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
/// <summary>
///
/// </summary>
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
