0

我的逻辑层中有一个函数可以通过另一个线程插入信息,如果该线程中发生未知异常,我希望抛出它,但问题是在我的应用程序UnhandledExceptionEventHandler中出现一般异常,它给出了这个错误,我也不想关闭应用程序:

调用线程必须是 STA,因为很多 UI 组件都需要这个

///////////////////// this is my function in logical layer //////////
public string MahaleInsert(Mahales Mahale)
{
    Thread t = new Thread(delegate()
    {
        try
        {
            Mahale.OutMessage = DA_Agency.MahaleInsertDB(Mahale);
            Mahale.OutMessage = "SuccessInsert";
        }
        catch (Exception ex)
        {
            if (ex.Message.ToLower().Contains("violation of unique key constraint"))
                Mahale.OutMessage = "MahaleInsUniqueError";
            else
                throw;
        }
    });
    t.Start();
    t.Join();
    return Mahale.OutMessage;    
}

//////////////////////////// this in my aplication level  //////////////////
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    try
    {
        string messsage = GeneralMethod.CatchException(e.ExceptionObject as Exception);
        MessageClass.MessageBox(messsage);
    }
    catch
    {


    }
}
4

1 回答 1

1

尝试执行以下操作:

    public string MahaleInsert(Mahales Mahale)
    {
        Thread t = new Thread(new ThreadStart(ThreadBody));
        t.Start();
        t.Join();
        return Mahale.OutMessage;
    }
    [STAThread]
    void ThreadBody()
    {
        try
        {
            Mahale.OutMessage = DA_Agency.MahaleInsertDB(Mahale);
            Mahale.OutMessage = "SuccessInsert";
        }
        catch (Exception ex)
        {
            if (ex.Message.ToLower().Contains("violation of unique key constraint"))
                Mahale.OutMessage = "MahaleInsUniqueError";
            else
                throw;
        }
    }

    //////////////////////////// this in my aplication level  //////////////////
    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        try
        {
            string messsage = GeneralMethod.CatchException(e.ExceptionObject as Exception);
            MessageClass.MessageBox(messsage);
        }
        catch
        {


        }
    }
于 2012-12-11T11:45:51.780 回答