Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 线程异常处理_C#_Wpf_C# 4.0 - Fatal编程技术网

C# 线程异常处理

C# 线程异常处理,c#,wpf,c#-4.0,C#,Wpf,C# 4.0,我在我的逻辑层中有一个函数,它通过另一个线程插入信息,如果该线程中发生未知异常,我希望抛出它,但问题是,在我的应用程序unhandledexceptionventhandler中,如果获得一般异常,它会给出此错误,我也不想关闭应用程序: 调用线程必须是STA,因为许多UI组件都需要它 ///////////////////// this is my function in logical layer ////////// public string MahaleInsert(Mahales Ma

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

调用线程必须是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
    {


    }
}

尝试执行以下操作:

    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
        {


        }
    }

你试过将你的线程设置为STA吗?