Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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



:)
Posted
Updated 1-Sep-10 5:48am
v2
Comments
Sandesh M Patil 1-Sep-10 11:43am    
did not understand please explain...provide some code
Sandesh M Patil 1-Sep-10 11:48am    
Modify question for better understanding

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.
 
Share this answer
 
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);


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


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:
 
Share this answer
 
Comments
AspDotNetDev 1-Sep-10 17:48pm    
How do you propose the exception would be passed up the call stack of thread 2 (they have different call stacks)? It would not, unless there was code in thread 2 that specifically re-threw the exception, as I indicated in my answer. And if it explicitly re-throws the exception, there is no chance of the scenario you indicated happening, unless the programmer explicitly makes it happen.
Abhishek Sur 3-Sep-10 15:25pm    
Hey,
I was talking about just that... It would be a horrible implementation if the user generates the exception threw from a thread. I just gave him a rough code to show how horrible the scenario can be. The code is not actual.
I was talking about http://blog.headius.com/2008/02/rubys-threadraise-threadkill-timeoutrb.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900