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
3
Question by Korenn · Mar 07, 2013 at 12:11 AM · editorthreadingbestpractices

How do you stop multithreading in editor?

We're using threads to do some heavy computation in the background. We know all about the Unity API not being thread safe so you don't have to worry about that ;)

Does anyone have some good practices on how to do multithreading? Because we've just started and already found some major pitfalls:

  • Exceptions on threads are ignored. There's not even a log message! We're going to have to write our own thread exception handling just because Unity is dumb.

  • Threads do not pause when you hit 'pause'. This was a bit of a shocker. Why are the threads not suspended? How can we detect that the game has been paused in the editor?

  • Threads do not end when you stop running the game. This is just mind-boggling. What? so threads aren't cleaned up resulting in all sorts of memory leaking every time a thread doesn't clean up properly? If a thread gets stuck in an infinite loop you'll just have to exit Unity entirely? How does anyone do development on a threaded application in this scenario?

I'm certain I'm not the first to notice this, so can anyone give me some pointers as to how to approach this?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Simon-O · Jul 18, 2015 at 12:58 AM

Spawned threads don't report unhandled exceptions in the .Net framework unless you explicitly choose to handle them one way or another. This makes sense as your main thread may well be mid-method when the exception is raised.

The simplest way to catch all Exceptions is to launch a thread as follows:

 Thread t = new Thread(() => {
     try {
         //Call your Work() method
     } catch(Exception e) {
         //Log
     }
 });
 
 t.Start();

There are also other places you can hook into eg the AppDomain but they're less explicit.

As an aside, you should always write exception handling for your threads!

Re: Not pausing... When you Pause() Unity just stops calling certain methods on your MonoBehaviours. It has nothing to do with actually pausing any code execution. Suspending all background threads would cause other problems (eg music halting).

If you retain a reference to your threads, you can work around this easily.

Old-school, use t.Suspend() and t.Resume() as required. A better way is to use a ManualResetEvent and check its status. This allows your current iteration of the background thread to complete before pausing. Example here

An application exits when all foreground threads have finished execution.

.Net doesn't know what you're using the thread for. It might be writing to disk and aborting mid-method could result in corruption.

The problem here is that you haven't told .Net that you're spawning a background thread that can be aborted safely.

 Thread t = ...;
 t.IsBackground = true;
 t.Start();

So, really, there's nothing wrong with the threading model, but you may benefit from a little more reading up on the complexities of multi-threaded code. It's a complex subject and probably the second hardest aspect of programming to get right after cryptography.

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

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

11 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

Related Questions

Threading in Unity Editor 1 Answer

Best strategies for not accidently editing whilst still running 8 Answers

How to clear UnityEditor memory? 0 Answers

Parse Database in WebGL 0 Answers

Creating a software for drawing diagrams 4 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