- Home /
Is there any way to catch coroutine ending?
Hi, All !!
I started to connect openCV (this is C++ library) with unity3D and recently have known StartCoroutine function. I thought that function is very useful and finally I implemented opencv call routine in the Coroutine.
code snippet is below
IEnumerator CameraRun() {
initCamera(cameraWidth, cameraHeight);
while(cameraRun) {
queryNextFrame(cameraDataPtr, cameraWidth * cameraHeight * 3);
yield return null;
// do something job
}
releaseCamera();
}
void OnApplicationQuit() {
cameraRun = false;
// releaseCamera();
}
But, I could not reach to the point releaseCamera() function calling. When OnApplicationQuit() function is called Coroutine stop suddenly and releaseCamera is never called. So I have to call releaseCamera in OnApplicationQuit().
Why I want to call initCamera() and releaseCamera() in Coroutine is I think it is safe all function callings which have relation to web camera device is in same thread.
Anyway, Is there way to catch coroutine ending. I want to release camera in Coroutine ( I Always do this way in real Thread body function )
PS) I have whole code implementation but I can't attatch file because of file size limitation
Answer by ABerlemont · May 04, 2017 at 08:51 AM
Another way to do something like this (title related) that someone might find useful would be to give a delegate in param of the IEnumerator
using System;
void call(){
StartCoroutine(CameraRun(releaseCamera));
}
IEnumerator CameraRun(Action callback) {
initCamera(cameraWidth, cameraHeight);
(..yielding stuff..)
if(callback != null) callback();
}
void releaseCamera(){
}
Answer by aldonaletto · Sep 07, 2012 at 05:09 PM
You can use a boolean flag to know when the coroutine has finished. In the example below, the game will run until cameraRun becomes false: this will end the coroutine loop, execute releaseCamera() and set the variable cameraActive to false. The code in Update detects when cameraActive becomes false and calls Application.Quit(), ending the game:
// this boolean flag is true while the camera is active:
public bool cameraActive = true;
IEnumerator CameraRun() {
initCamera(cameraWidth, cameraHeight);
while(cameraRun) {
queryNextFrame(cameraDataPtr, cameraWidth * cameraHeight * 3);
yield return null;
// do something job
}
releaseCamera();
cameraActive = false;
}
void Update(){
if (cameraActive == false){
// camera deactivated: you can finish the application:
Application.Quit();
}
}
Hi Aldo, thanks for your answer. But if cameraActive flag is set to false it means releaseCamera() function also was called. cameraRun flag is always true whole time of game running and it must be false only when game should be stopped. (If you want to see whole code I can send you via email willingly)
Well, this is just a suggestion - I don't know exactly which's the desired sequence. In this code, setting cameraRun to false starts the game stopping process: it will finish the coroutine loop, execute releaseCamera() and finally clear cameraActive to signal the coroutine end - this flag is monitored in Update, which in turn will call Application.Quit()
Actually, it would be smarter to call Application.Quit() right after executing releaseCamera() in the coroutine, but I wanted to emphasize the use of a boolean flag to signal the coroutine end.
There are two situation. One is when pressing keyboard key (like Esc key..) and another is when clicking stop button in unity3d editor. If I press down key I can capture key event and your solution is great. But if I click stop button in unity3d editor I don't know exact procedure to stop coroutine. ( When I using thread I can control stop procedure by calling thread.join function. )
Answer by $$anonymous$$ · May 16, 2013 at 10:06 AM
You can hijack the IEnumerator like this:
IEnumerator someCoroutine = aCoroutine();
Coroutine startSomeCoroutine = StartCoroutine(someCoroutine );
while (someCoroutine.MoveNext())
{
Debug.Log("Im running");
yield return null;
}
yield return startSomeCoroutine;
So when the loop is over, the routine is done.
This works when you have a "yield return null" inside your "someCoroutine" but if you want to use a "yield return new WaitForSeconds([num]);" then it doesn't.
As usual, I'm a bit late to the party, but for all those wanting to use this in the future: ins$$anonymous$$d of returning null (which may not work, in the event that the Coroutine wants to return a WaitForSeconds, as mentioned by ABerlemont), you can just yield return the actual value the coroutine wants to return. For example:
IEnumerator someCoroutine = aCoroutine();
Coroutine startSomeCoroutine = StartCoroutine(someCoroutine );
while (someCoroutine.$$anonymous$$oveNext())
{
Debug.Log("Im running");
yield return someCoroutine.Current;
}
yield return startSomeCoroutine;
IEnumerator.Current (in this case, the IEnumerator is someCoroutine) gets the current value the coroutine is currently yield return -ing. This way, WaitForSeconds and any other value the "hijacked" enumerator may return, gets passed through.
Your answer
