- Home /
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.
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.
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 !
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.
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.
Your answer
Follow this Question
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