六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

開心,捕捉子線程內異常的例子

[摘要]程序很簡單,一個form內有一個按鈕,點擊后彈出一個對話框,點擊對話框start按鈕后執行新起一個線程執行一個工作類的某個方法,這個方法故意拋出異常,然后在對話框內捕捉到這個異常。注意,這兒我是用事件來捕捉異常,這只是其中一種方法form 的代碼using System;using System....
程序很簡單,一個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



主站蜘蛛池模板: 人人干狠狠干 | 色噜噜狠狠狠色综合久 | 欧美无专区 | 欧美又大又粗又长又硬 | 综合色天天| 亚洲欧洲色天使日韩精品 | 手机看片国产精品 | 人人爽天天碰天天躁夜夜躁 | 青青青青爽极品在线视频 | 天天干天天添 | 天天天狠天天透天天制色 | 青娱乐在线视频免费观看 | 中文字幕亚洲无线码在一区 | 天天干夜夜艹 | 欧美一区二区三区四区五区六区 | 婷婷六月丁香色婷婷网 | 亚洲日本va中文字幕 | 欧美一级淫片a免费播放口aaa | 色综合欧美色综合七久久 | 青春草在线观看播放免费视频 | 日韩免费观看 | 最新韩国伦理片大全手机在线播放 | 亚洲永久精品网站 | 欧美午夜视频 | 又粗又硬又爽的三级视频 | 日韩福利在线 | 亚洲欧美人妖另类激情综合区 | 欧美一区二区免费 | 深夜福利视频导航 | 欧美一级纶理片免费 | 日本vs欧美一区二区三区 | 日韩黄色影院 | 亚洲一区免费在线观看 | 亚洲天堂视频在线观看 | 亚洲精品免费在线观看 | 日韩不卡毛片 | 日日摸夜夜添夜夜添成人 | 日本男女网站 | 亚洲精品乱码久久久久久麻豆 | 日产国语一区二区三区在线看 | 欧美视频在线免费看 |