正在运行的线程引发异常 [英] Throw Exception from running thread

查看:57
本文介绍了正在运行的线程引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Web应用程序,其中我已在import.aspx页中为thread编写了代码.

在该thread中,我要将filelist作为参数传递给包含路径详细信息的文件的函数.

现在在BO project中,我正在使用该文件列表作为iteration.

如果file not found我想throw exception进入UI,但问题是我无法抛出exception,尽管我在catch block
中获取了exception.


:)

I am using web application where I have written code in import.aspx page for thread.

In that thread I am passing filelist as parameter to the function which contains file with their path details.

Now in BO project I am using that file list for iteration.

If file not found I want to throw exception to UI, but the problem is I am not able to throw the exception, though i m getting the exception in catch block



:)

推荐答案

您要尝试执行的操作是ASP.NET程序无法像Windows Forms程序那样连续运行.不能简单地调用Invoke()在"UI"线程上执行代码.考虑一下UI是什么...呈现给客户端的HTML.并且服务器不启动渲染...客户端回发并且服务器响应该请求.因此,如果您正在运行此额外的线程,则可能已经响应了该回发,在这种情况下,您就是SOL.

但是,仍然有希望.您可以做的是运行线程,然后阻止主代码继续执行,直到该线程完成为止.您可以为此在线程类上使用Join()方法(如果您愿意,可以在调用Join()之前进行其他处理……您只需调用Join()以确保其他线程已完成) .现在,在该线程中,您可以在import.aspx代码隐藏类上分配一个变量,以记住该线程运行时可能发生的任何异常.因此,线程将捕获异常,将其分配给变量,然后线程结束,主线程将恢复并读取该变量,然后引发异常,该异常将传播给客户端.
The problem with what you are trying to do is that ASP.NET programs don''t continuously run as a Windows Forms program does, so you can''t simply Invoke() to execute code on the "UI" thread. Think about what the UI is... the HTML that gets rendered to the client. And the server doesn''t initiate the render... the client posts back and the server responds to that request. So if you have this extra thread running, that postback may have already been responded to, in which case you are SOL.

However, there is still hope. What you can do is run the thread and then prevent your main code from continuing until that thread completes. You can use the Join() method on the thread class for this (you can do some other processing before calling Join() if you like... you''d just call Join() to be sure the other thread has finished). Now, in that thread, you can assign a variable on the import.aspx code-behind class to remember any exception that may have occurred while the thread was running. So the thread would catch the exception, assign it to a variable, then the thread would end, the main thread would resume and read that variable and would then throw the exception, which would get propogated to the client.


相信我,从一个线程向另一个线程抛出异常不是一个好主意.

考虑场景:

线程1

Believe me, it is not a good idea to throw an exception from one thread to other.

Consider the scenario :

Thread 1

throw new FileNotFoundException ("Err... Cant see the file!!", ex);



线程2:



Thread 2 :

try 
{
   //doing task
}
catch {}
finally
{
  clearStuffs()// 
 //Now say at this point Thread 1  throws the error. 
}



因此,您可以看到,如果您的应用程序确实向另一个线程抛出了异常,则可能会使整个应用程序完全崩溃.

因此,与其执行此操作,不如更好地处理异常,并在发生异常时从线程中调用委托,并执行一些步骤以显示线程中发生了什么错误.
:thumbsup:



So you can see, if your application does throw the exception to the other thread, it might crash the whole application altogether.

So rather than doing this, I think it is better handle the exception and invoke a delegate from the thread when exception occurs and execute few steps to show what error occured in the thread.
:thumbsup:


这篇关于正在运行的线程引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆