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 judy3turn · Aug 28, 2013 at 02:34 AM · coroutinethreads

How do I run a coroutine in the non-main thread?

I want to run saves to my database periodically instead of every time the data is updated. I want to run it sort of like a garbage collection routine. Sleep X seconds, run, sleep X seconds, run.

I tried using repeatInvoking first but it can only run from the main thread. So then I decided to use a coroutine. It works fine as long as I don't put in the yield statement, but as soon as I add Yield it simply does not run.

in Awake()

 savePis = new Thread(DoSaves);
 savePis.Start();

void DoSaves()

 {
     Debug.Log("Calling SaveToDB ");
     SaveToDB(); // because you cannot start a thread with an IEnumerator
 }

 IEnumerator SaveToDB()
 {
     Debug.Log("STARTING SAVETODB....");
     Debug.Log("YEILDING ..... ");
     yield return new WaitForSeconds(1);        // because invoke repeating only works in the main thread
     Debug.Log("FINISHED YEILDING");
 }

The Debug.Log never prints Starting SaveToDB. But if I switch the yield statement for a return null, it prints out great.

I'm not calling ANY unity APIs here so why is the yield failing? I thought it might be Debug.Log that was failing, but changing them to print doesn't help.

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 Peter G · Aug 28, 2013 at 02:57 AM

I'm a little curious why you are pushing this operation to another thread?

But for starters, the delayed execution functionality of yield that Unity only works if you call it from StartCoroutine(). Otherwise it's just a regular C# yield. You can still yield return WaitForSeconds, it just won't do anything meaningful.

I would recommend keeping time in the main thread then pushing this save feature over to a second thread only when you need it.

 void Start () {
       StartCoroutine( SaveToDB() );
 }
 
 IEnumerator SaveToDB () {
 
      while (true) {
            yield return new WaitForSeconds(1);
            savePis = new Thread(DoSaves);
            savePis.Start();
      }
 }

That's just a quick set up. I'd probably use the thread pool over creating a new thread, but you probably know your system better than me.

Comment
Add comment · Show 4 · 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 Pikasha · Mar 22, 2021 at 02:29 PM 2
Share

He probably is doing this to be able to add a big file to a database without the application being frozen until the operation is done. At least, this is the problem I am having that I am trying to fix. If you have any solution for this thanks to share it !

avatar image Captain_Pineapple Pikasha · Mar 22, 2021 at 02:43 PM 1
Share

there is really no point in adding a comment to a 8 year old answer mate. If you have a question open it as a new question and reference this as information that you have read.

avatar image Pikasha Captain_Pineapple · Mar 22, 2021 at 03:50 PM 0
Share

Since you responded, it shows that people still watch the post and could possibly answer. It is true that I could make a new post but here it notifies the person that gave interesting information.

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

19 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

Related Questions

Threading in Unity 6 Answers

Crash using Sockets 2 Answers

Return value from coroutine to non monobehaviour 1 Answer

Coroutine Death on Scene Change 3 Answers

How to handle a slow function from a dll 1 Answer


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