Click here to Skip to main content
       

C#

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page  Show 
GeneralRe: Error in connection stringmemberMember 964140618 Feb '13 - 4:53 
there is appear in server explorer the database ozeki.mdf which i have created in my application, but i couldn't make the correct connection to it ,,, i copy the connection string from ozeki.mdf property which contain the full path but it result the above error ,,,
GeneralRe: Error in connection stringmemberdeflinek18 Feb '13 - 22:32 
Is it asp.net, desktop or metro application?
--
"My software never has bugs. It just develops random features."

QuestionEvil Thread.Abort and using(TransactionScope) -- what will happen?memberdevvvy18 Feb '13 - 2:41 
hi,
 
I dig around [^]understand that depending on compiler architecture using statement is actually a syntactic sugar ...
 
Translate FROM:

using (TransactionScope scope = new TransactionScope())
{
...
}

 
TO:

TransactionScope scope = new TransactionScope();
#PT1#
try {
...
#PT2#
scope.Complete();
#PT3#
} finally {
if (scope!= null)
((IDisposable)scope).Dispose();
}

 
Previously, as I read, depending on compiler architecture code may blow up at #PT1# and thus finally cleanup block would never execute. But again, I read further that Microsoft already fixed this problem.
 
What I want to ask is, is using(TransactionScope) now Thread.Abort safe? If code blows up at #PT2# and not #PT3#, would "scope.Dispose" rollback the transaction as well?
 
Thanks
dev

AnswerRe: Evil Thread.Abort and using(TransactionScope) -- what will happen?memberBobJanova18 Feb '13 - 2:59 
At PT1 you haven't actually done anything with the scope, so even if it doesn't get cleaned up immediately, you haven't done anything which could damage your database.
 
What happens if it dies at PT2 is dependent on TransactionScope.Dispose, but I'd assume it does the right thing.
 
Thread.Abort is a blunt instrument, though, so it should only be used in extreme cases.
AnswerRe: Evil Thread.Abort and using(TransactionScope) -- what will happen?mvpEddy Vluggen18 Feb '13 - 3:01 
devvvy wrote:
What I want to ask is, is using(TransactionScope) now Thread.Abort safe?

Not as far as I know (cannot test now), and shouldn't be. Terminating is exactly that - terminating. No cleanup, nothing gracefull, but an exit.
Bastard Programmer from Hell Suspicious | :suss:
If you can't read my code, try converting it here[^]

AnswerRe: Evil Thread.Abort and using(TransactionScope) -- what will happen? [modified]protectorPete O'Hanlon18 Feb '13 - 4:51 
No, you can't guarantee that the Dispose method will be reached with Thread.Abort - this applies to pretty much any disposable class wrapped with using, and is not specific to TransactionScope. To understand why this is the case, it's important to understand that Thread.Abort is triggered as an asynchronous exception (unlike most other exceptions which are synchronous). This means that there is no guarantee as to when the exception is raised.
 
As you've pointed out, the using pattern is effectively syntactic sugar for try/finally, and I'm a really big fan of it. Thread.Abort, however, tramples over this sandbox because of a nasty little timing issue. If your code is lucky enough to abort during the bit in the body of the using statement, then the finally part, and that's a fine thing. If, however, your code has just to say entered the finally portion and the abort exception is raised, the code will drop out of the finally block at the point that it encounters the exception - even if it hasn't called Dispose yet.
 
However, apart from forcing termination of the program, I have never seen a system that really needs a Thread.Abort as there are generally other, safer methods to cancel a thread. Use of Thread.Abort is generally recommended by people who aren't aware that there are issues with using it, as it is an easy method to implement.
I was brought up to respect my elders. I don't respect many people nowadays.

CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier


modified 18 Feb '13 - 12:24.

GeneralRe: Evil Thread.Abort and using(TransactionScope) -- what will happen?memberdevvvy18 Feb '13 - 5:23 
Pete O'Hanlon wrote:
If, however, your code has just to say entered the finally portion and the abort exception is raised, the code will drop out of the finally block at the point that it encounters the exception

 
Thanks very much Peter!
dev

GeneralRe: Evil Thread.Abort and using(TransactionScope) -- what will happen?protectorPete O'Hanlon18 Feb '13 - 5:25 
Don't mention it. I'm glad to help, and this is the type of esoteric "how does it really work" question that I enjoy answering.
I was brought up to respect my elders. I don't respect many people nowadays.

CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

GeneralRe: Evil Thread.Abort and using(TransactionScope) -- what will happen?memberdevvvy18 Feb '13 - 5:53 
having the bomb dropped during entry to Finally block was something that I never seen in other articles (many of which full of half answers), do I am actually quite delighted that I can finally put this question to rest.
dev

GeneralRe: Evil Thread.Abort and using(TransactionScope) -- what will happen?memberjschell18 Feb '13 - 9:01 
Pete O'Hanlon wrote:
I have never seen a system that really needs a Thread.Abort

 
Curious about that.
 
A windows service that is supposed to 'stop' will have difficultly actually doing that if normal threads continue to run. So something which doesn't have control over a child thread and which has already attempted to nicely stop the thread can use Abort to force the issue.
 
Is there an alternative?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 24 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid