Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Paulius-Liekis · Nov 20, 2014 at 09:59 PM · windowsthreadtry-catch

finally is not executed when calling Thread.Abort

Does anyone know a reason why finally statement wouldn't be executed when calling Thread.Abort? I have thread function like this:

 void ThreadFunc()
 {
     try
     {
         int counter = 0;
         while (true)
         {
             Debug.Log ("Waiting" + counter);
             Thread.Sleep(100);
             ++counter;
         }
         Debug.Log("Success.");
     }
     catch(ThreadAbortException ex)
     {
         Debug.LogWarning("Cancelled: " + ex);
     }
     catch(System.Exception ex)
     {
         Debug.LogError("Error: " + ex);
     }
     finally
     {
         Debug.Log("Finally");
     }
 }

I terminate this thread using Thread.Abort. All I see in output is a bunch of Waiting, but no other lines. My main thread keeps running after calling Thread.Abort (i.e. child thread has enough time to complete).

This happens on Windows Editor. On Mac Editor it works fine. On iOS I get this behavior, but it seems to depend on device/iOS version or XCode version (I haven't figured out yet). On Windows it happens every time.

BTW: on iOS it crashes if I put another try {} catch {} into finally statement.

Is anyone aware why this is happening? It seems to be quite different from what the documentation of Thread.Abort and ThreadAbortException says.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoyo · Oct 19, 2015 at 06:46 PM 0
Share

In my testing it appears that (a) none of Thread.Abort, Thread.Interrupt, and Thread.Suspend do anything unless the thread being interrupted calls Thread.Sleep, and (b) even if the thread does sleep, it does not get a chance to catch a ThreadAbortException or even run a finally block. Thread.Interrupt does result in a ThreadInterruptException, but only after the thread sleeps. Seems like multiple bugs, probably because of the old / custom version of $$anonymous$$ono that Unity is using.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Paulius-Liekis · Nov 21, 2014 at 01:51 AM

StackOverflow gave quite precise answer:

Mono does not support Constrained Execution Regions and does not fully support catch/finally when Thread.Abort is called.

There is a bug about this.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Bunny83 · Nov 20, 2014 at 10:48 PM

You might want to take a look at this and turn away from Abort. Threads should never be aborted from outside and if you look through the answers you will see that Abort isn't very reliable.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Paulius-Liekis · Nov 20, 2014 at 11:10 PM 1
Share

Well, the documentation of Thread.Abort doesn't hint anything that it should not be used. And I have no problem with complicated behavior of Abort and ThreadAbortException as long as it follows some specific documentation. At the moment behavior seems to vary from platform to platform.

avatar image Paulius-Liekis · Nov 20, 2014 at 11:14 PM 0
Share

I haven't seen any good alternatives for Thread.Abort which doesn't make your code messy and can actually halt operation which are happening not in your code. And I'm taking about real world thread implementation which makes N connections to the net and $$anonymous$$ loops in different places.

avatar image Bunny83 · Nov 21, 2014 at 12:01 AM 0
Share

Have you actually read the entire remarks section of Thread.Abort?

The thread is not guaranteed to abort immediately, or at all

This method is actually called when your program exits. The documentation just explains the bare functionality but don't suggest when it might be used.

You might also want to read the answer of Eric Lippert (in case you don't know him, he's one of the guys who wrote the C# compiler at $$anonymous$$S).

avatar image Paulius-Liekis · Nov 21, 2014 at 01:41 AM 1
Share

This is going off topic. $$anonymous$$y question is why neither catch(ThreadAbortException) nor finally are hit. None of the links explain why finally is not executed. They all talk that you're not guaranteed that thread will ter$$anonymous$$ate, and they imply that if thread is ter$$anonymous$$ated, then finally is called (i.e. the behavior should be according to the documentation). But that's not what I'm getting.

I can see myself that Thread.Abort is not a reliable thing, but I'm saying that implementation in $$anonymous$$ono doesn't follow behavior that is described in documentation. The situations on StackOverflow can be explained, by documentation or wrong implementation, but not in this case.

avatar image Paulius-Liekis · Nov 21, 2014 at 05:06 PM 1
Share

Have you read Thread.Abort and ThreadAbortException documentation? The "usually" part is quite well defined. The reason why it says "usually" it is because it depends on user's code - your code might never leave finally clause if you want to. The other case where unmanaged code doesn't return, but in that case the alternative methods do not work.

If you ter$$anonymous$$ate a thread forcefully, then yes - bad things can happen (mostly resources left in unknown state). But if it throws an exception and all finally/using clauses are executed properly, then what's the problem exactly?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Speech to text 2 Answers

Mac OSX on windows and building out for iphone/ipad? 2 Answers

Detecting mulitple xbox controllers on Windows - randomly gets joystic number. 0 Answers

How to hide command prompt console? 0 Answers

Micosoft's Halolens/Continuum 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges