有关多线程错误处理

Posted on 2007-05-20 23:40  逍遥散人  阅读(793)  评论(0编辑  收藏  举报

引用于:http://community.csdn.net/Expert/topic/5375/5375281.xml?temp=.1688806

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace 经典测试
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(
            string lpClassName,
            string lpWindowName
        );

        [DllImport("user32.dll", EntryPoint = "GetWindowText")]
        public static extern int GetWindowText(
            IntPtr hwnd,
            string lpString,
            int cch
        );

        [DllImport("user32.dll", EntryPoint = "SetParent")]
        public static extern int SetParent(
            IntPtr hWndChild,
            IntPtr hWndNewParent
        );

        private void Form2_Load(object sender, EventArgs e)
        {
            //word class OpusApp

            IntPtr hWord =  FindWindow("OpusApp", null);
            if (hWord != IntPtr.Zero)
            {
                SetParent(hWord, this.Handle);
                foreach (object frm in this.Controls)
                {
                    if (frm is Form)
                    {
                        MessageBox.Show((frm as Form).Text);
                    }
                }
            }
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Threading.ThreadStart ts = new System.Threading.ThreadStart(MyThread);
            MySafeThread th = new MySafeThread(ref ts, new object[] { "nothing to write" });
            th.ThreadException += new MySafeThread.ErrorHappendEventHandler(th_ThreadException);
            th.BeginThread();
        }

        void th_ThreadException(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        private void MyThread()
        {
            while (true)
            {
                throw new System.AccessViolationException("My test exception message here.");
            }
            MessageBox.Show("thread run over.");
        }
    }

    //以下这个类就是关键
    public class MySafeThread
    {
        private object[] m_params;
        private System.Threading.Thread m_th;
        private System.Threading.ThreadStart userThread;
       
        public delegate void ErrorHappendEventHandler(Exception ex);
        public event ErrorHappendEventHandler ThreadException;

        //如果你自己想启动一个带参数的安全线程,那你自己写个委托替换掉下面这个
        //System.Threading.ThreadStart就可以了。
        public MySafeThread(ref System.Threading.ThreadStart ts, object[] Params)
        {
            userThread = ts;
            m_params = Params;
        }

        public void BeginThread()
        {
            if (m_th != null)
            {
                if (m_th.IsAlive)
                {
                    m_th.Abort();
                }
            }
            m_th = new System.Threading.Thread(new System.Threading.ThreadStart(InitThreadProc));
            m_th.Start();
        }

        public void EndThread()
        {
            if (m_th != null)
            {
                m_th.Abort();
            }
        }

        private void InitThreadProc()
        {
            try
            {
                userThread.Invoke();
            }
            catch(Exception ex)
            {
                if (ThreadException != null)
                {
                    ThreadException(ex);
                }
            }

            MessageBox.Show("MyThread run over");//最终这行你就可以删除了。
        }
    }
}





//调用方法:
假设线程函数是这样的:
private void MyThread()
{
        while (true)
        {
                throw new System.AccessViolationException("My test exception message here.");
        }
        MessageBox.Show("thread run over.");
}

抛出异常是模拟了你线程处理过程中发生了异常的情况。
void th_ThreadException(Exception ex)
{
        MessageBox.Show(ex.Message);
}
这个函数捕获并提示了异常信息,信息你可以自己定义

System.Threading.ThreadStart ts = new System.Threading.ThreadStart(MyThread);
MySafeThread th = new MySafeThread(ref ts, new object[] { "nothing to write" });
th.ThreadException += new MySafeThread.ErrorHappendEventHandler(th_ThreadException);
th.BeginThread();

Copyright © 2024 逍遥散人
Powered by .NET 8.0 on Kubernetes