|
devvvy wrote: Thought I said ThreadAbortException...
You did. You also asked if you could catch all exceptions. The one I posted cannot be caught.
The ThreadAbortException (not what I mentioned) is special both it what it does to a thread and what it does on exit from the catch block (excluding special handling.)
|
|
|
|
|
I would expect some exceptions to be thrown within the threads and some thread related exceptions to be thrown back to the main thread; ThreadAbortException could be one of the later.
In the later case, I would expect the following code to catch all exception thrown within the main thread:
[STAThread]
[SuppressMessage("Microsoft.Usage", "CA1801")]
public static int Main(string[] args)
{
Program program = new Program();
do
{
try
{
program.Update();
Application.DoEvents();
}
catch (Exception exception)
{
if (program.Log.IsFatalEnabled)
{
program.Log.Fatal("Caught Unhandled Exception: " + exception);
}
break;
}
}
while (program.StateCurrent != EProgramState.Shutdown
&& program.ExitCode == EProgramError.NoError);
return (int)program.ExitCode;
}
If ThreadAbortException is thrown within the thread, I would look for a way to register my own thread main method and do similar wrapping in there.
Kind Regards,
Keld Ølykke
|
|
|
|
|
Hi,
I have two try blocks, two catches and one finally block.
I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program.
If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open.
How can I force program to go to finally block wit hexecuting the remaining code?
|
|
|
|
|
if you exit the try block it would automatically pass through finally block no matter how it exits the try block i.e either through catch or through return statement.
have you tried to debug this. you may paste the code block if it still didnt work.
Jibesh V P
|
|
|
|
|
I am going to use a return. But this exits the whole program. So, within this condition, I am going to close the connection and have a return.
|
|
|
|
|
what makes the program to exit? is that how it has to work?
Quote: So, within this condition, I am going to close the connection and have a return since you want to close the connection before the application exists thats the best place to do this reset.
Jibesh V P
|
|
|
|
|
If the database returns 0 rows, exit else continue.
|
|
|
|
|
Ok. did I answer your question? if not can you please share the code for the better understanding of your problem.
Jibesh V P
|
|
|
|
|
Yes, I think the general concensus is that, put try-catch-finally in all thread functions as coding practice.
dev
|
|
|
|
|
vanikanc wrote: If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open.
It will not end the program, but terminate it.
Use Application.Terminate for a WinApp, and simply return from the Main method (the entry-point) if you're writing a Console App.
|
|
|
|
|
vanikanc wrote: two try blocks, two catches and one finally block
You'd have to show the code for us to understand how you have them arranged.
vanikanc wrote: closed the connection to database
You should be using a using statement as well -- it's similar to a try/finally.
vanikanc wrote: Environment.Exit();
Don't do that; just return .
|
|
|
|
|
vanikanc wrote: then it will end the program, and I would not have closed the connection to
database that is open.
When the program terminates the connection will be terminated as well.
And the only way you can terminate a database connection in C# anyways is if all of the following are true.
1. You have configured the connection pool to keep no idle connections.
2. There are no connections in use
3. You reset the pool.
Other than that just "closing" the connection does nothing more than return it to the connection pool.
|
|
|
|
|
Just return from the method. The finally block will execute and then your method will return.
|
|
|
|
|
Some alternate ideas.
- Are the two try/catch absolutely necessary? Try/catch is an expensive operation and I see many people using it where a simple if statement could save the day.
- Perhaps you can remove the second try, but move the catch to the first try. you can "assign" multiple catch handlers to one try. Of course only valid if you specify the exceptions. eg.:
try{
}
catch(SqlExcpetion sqlex){
}
catch(IOException){
}
catch(Exception ex){
}
finally{
}
- the quick and dirty way is maybe a goto statement, but you don't want to go that way, they're evil
hope this helps.
|
|
|
|
|
Just write return and the function will end after executing finally block.
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore, Dream. Discover.
|
|
|
|
|
One suggestion from my side. If you put you connection inside a using block, you will not have to rely on any specific piece of code to close the connection and you can simply return from anywhere. It is the recommended way of doing the connection business.
This article will tell you how to use the connections with a using
ASP.NET - How To Use(Open/Close) Connections Correctly[^]
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore, Dream. Discover.
|
|
|
|
|
I know what happens to lock(x) in event of a Thread.Abort, what about "using"?
dev
|
|
|
|
|
UNIT CONVERSION APPLICATION:
Build a WCF web service (ConvertService) having following methods:
(a) GetUnits() method to retrive available units
(b) Convert() method to convert a measure from selected units handling errors (eg. attempt to convert inches to kilogram.. etc.)
(c) GetRatio() method returning a ratio used in conversion from/to selected unit
Should cover at least four units:
Mass and weight- Kilogram(kg) and pound (lb)
Distance and length - Kilometers(km), miles(mi), centimeters(cm) and inches(in)
Capacity and volume - litre(l) and gallon (gal)
Temperature - Celsius scale (C) and fahrenheit scale (F)
Create a WCF service client (ConvertClient) which windows form client having following interfaces which uses the WCF service
From(lbl): textbox (input) ----To(lbl): textbox (result)
UnitFrom(unit select + combobox) ------UnitTo(unit select + combobox)
<-->
(btn to switch)
Convert(btn) Ratio: textbox
Get Ratio(btn)
Thanks.
|
|
|
|
|
Temperature doesn't have a conversion ratio.
|
|
|
|
|
Thanks for the reply...
I would like to know What time would it take and what are your requirements (charge)?
|
|
|
|
|
You may be in the wrong forum. However, if you want me to do it for you it would cost $2,500 USD prepaid and it would be done in about an hour.
|
|
|
|
|
|
All Correct
|
|
|
|
|
MacRuturaj wrote: Thanks.
For what? It cannot be for answering your question, as there isn't one. We can help you with specific questions, but we don't do complete projects. CodeProject is a community, not a software vendor.
FWIW; you might want to reconsider the design. You can cover almost any unit by multiplying by the correct factor. Doesn't work for temperature though; easiest way was to add the multiplication-factors (and unit-names) to a table, and add in a formula that gets Eval uated for the temperatures.
Bonus-points if you can keep all your stuff in their respective SI-base units (so no kilometers or centimeters, only meters)
|
|
|
|
|
@Eddy Vluggen- I can understand that....
I posted this question because I saw some complete projects available for download on this website, I was looking through some material I can get and I found 2 softwares... so I thought I might get some answers like a software or a complete explanation with the code to make this small program.
|
|
|
|