Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by laradov · Nov 06, 2015 at 08:18 PM · pathfindingcoroutinesthreadsthreadingprocess

Best way to run a long algorithm in the background?

Hi, I have a pathfinding algorithm and it takes so much time to run. It is a function that returns a node list. I tried coroutines and threads but I couldn't make it work properly. I just want to make it work in the background when I call it

 start_process_in_the_background
 Path = GetPath();
 finish_background_process

I can start a thread in the background but I couldn't abort it when the algorithm finishes. I tried coroutines but they freeze the game during the algorithm. To sum up, what is the best way to call a function in the background?

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
0

Answer by insominx · Nov 06, 2015 at 09:08 PM

The only way to truly run something in the background is with a separate thread. Unfortunately the behavior of abort has not been something you can consistently rely upon for every platform (YMMV). You'll need to use a bool variable to signal the thread when it should finish. Something like:

 // inside thread function
 while (myThreadEnabled) {
    // ... do stuff
 }
 
 // outside thread function
 myThreadEnabled = false; // tell thread to finish

Once the thread function was exited naturally, it will terminate.

Comment
Add comment · Show 2 · 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 laradov · Nov 06, 2015 at 09:14 PM 0
Share

I used this but when I close the game Unity freezes since the thread still runs in the background. If you don't abort the thread, it is always a problem.

avatar image insominx laradov · Nov 09, 2015 at 04:31 PM 0
Share

You may need to do a thread join on exit. In my code I have something like this:

  void OnApplicationQuit() {
         if (externalThread!= null) {
             shouldExitThread= true;
     
           // Give the thread a brief moment to complete
           Thread.Sleep(100);
           externalThread.Join();
           externalThread= null;
         }
     }

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

32 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 avatar image avatar image avatar image avatar image

Related Questions

Why Is This Coroutine Freezing my Unity5 editor? 2 Answers

Heavy use of Unity API & UI Thread 0 Answers

Infinite loop with named pipes 1 Answer

Loading textures without freezing at runtime 0 Answers

Synchronize main Thread with worker Thread 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