• 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
2
Question by Gidools · Sep 07, 2012 at 03:53 PM · opencvstopcoroutine

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

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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();
   }
 }


Comment
Add comment · Show 3 · 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 Gidools · Sep 07, 2012 at 10:03 PM 0
Share

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)

avatar image aldonaletto · Sep 07, 2012 at 10:23 PM 0
Share

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.

avatar image Gidools · Sep 07, 2012 at 11:10 PM 0
Share

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. )

avatar image
1

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.

Comment
Add comment · Show 3 · 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 ABerlemont · Feb 18, 2016 at 01:40 PM 1
Share

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.

avatar image Hermanoid · May 04, 2017 at 03:13 AM 1
Share

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.

avatar image Kjaka · Feb 24, 2019 at 11:21 AM 0
Share

beautiful.

avatar image
7

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(){

 }

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

import video from camera 3 Answers

Unity 4.5 subview on Android with OpenCV 0 Answers

How do I send a screenshot to another application? 0 Answers

Opencv2.framework not loaded? 0 Answers

I'm stopping coroutines in pause but can't bring them back 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges