我有一个单独的线程运行的方法.该线程是从Windows应用程序中的表单创建和启动的.如果从线程内部抛出异常,将其传递回主应用程序的最佳方法是什么.现在,我将对主窗体的引用传递给线程,然后从线程调用该方法,并使该方法被主应用程序线程调用.是否有最好的练习方法,因为我现在对自己的表现不满意.
我的表格示例:
public class frmMyForm : System.Windows.Forms.Form
{
/// <summary>
/// Create a thread
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnTest_Click(object sender, EventArgs e)
{
try
{
//Create and start the thread
ThreadExample pThreadExample = new ThreadExample(this);
pThreadExample.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName);
}
}
/// <summary>
/// Called from inside the thread
/// </summary>
/// <param name="ex"></param>
public void HandleError(Exception ex)
{
//Invoke a method in the GUI's main thread
this.Invoke(new ThreadExample.delThreadSafeTriggerScript(HandleError), new …Run Code Online (Sandbox Code Playgroud)