Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2007

Tip: Looking for answers? Try searching our database.

managed threading / native exception handling question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
osudude - 12 Jun 2007 04:32 GMT
What happens in the following scenario?

1.Via a DLL interface, a managed thread kicks off a potentially long running
FOR loop operation encapsulated by a native C++ dll.  This FOR loop has C++
try/catch logic and allocates memory using calloc.
2. The managed thread is aborted before step 1 can complete.

In a pure managed environment, you would catch ThreadAbortException and
handle it appropriately (log error and do clean up).   Is some kind of
similar exception thrown in the C++ dll to indicate thread abortion or is
the thread just killed with no opportunity for exception handling / clean up
logic?

thanks!

ScottM
Oleg Starodumov - 12 Jun 2007 16:37 GMT
> What happens in the following scenario?
>
[quoted text clipped - 5 lines]
> clean up).   Is some kind of similar exception thrown in the C++ dll to indicate thread abortion or is the thread just
> killed with no opportunity for exception handling / clean up logic?

The thread will not be terminated, and there will be no exception until
the long operation has been completed and control has been returned to
managed code. Only after that ThreadAbortException will be thrown
in the target thread.

Signature

Oleg
[VC++ MVP http://www.debuginfo.com/]

adebaene@club-internet.fr - 13 Jun 2007 10:40 GMT
> What happens in the following scenario?
>
[quoted text clipped - 8 lines]
> the thread just killed with no opportunity for exception handling / clean up
> logic?

>From the Thread.Abort documentation :

"If, while executing unmanaged code, a thread ignores a
ThreadAbortException, the system re-throws the ThreadAbortException
when the thread begins executing managed code."

Arnaud
MVP - VC
Ben Voigt [C++ MVP] - 13 Jun 2007 18:14 GMT
>>From the Thread.Abort documentation :
>
> "If, while executing unmanaged code, a thread ignores a
> ThreadAbortException, the system re-throws the ThreadAbortException
> when the thread begins executing managed code."

And I think that is only applicable to a callback into managed code.  Trying
to abort from another thread won't cause any ThreadAbortException at all
until the target thread returns to managed code.  .NET Framework pulls some
tricks, like rewriting the return address on the stack placed by the call
into unmanaged code (and does this for GC as well), that is part of why
there is overhead for transitions between managed and unmanaged.  But it
definitely does not trigger an exception asynchronously, that isn't possible
without writing directly to internal OS data structures (kernel level access
required I believe) and is *very* unsafe.

> Arnaud
> MVP - VC

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.