Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
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
8

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 · Show 1 · 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 halivudestevez · Feb 04, 2021 at 07:24 PM 0
Share

perfect! thanks!

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.

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

12 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

Related Questions

Emgu/OpenCV with Unity Free 1 Answer

Snapchat's lenses skin smoothing effects in unity3d? achievable by shaders? 1 Answer

Is there any free tool for Face detection ? 0 Answers

Is there any performance differences between Coroutine methods ? 3 Answers

Memory overflow using OpenCV Imported with 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