how to shortcut a coroutine in unity C#? : I write a coroutine to do a batch work every frame, I have no problem to start it, but, I want to short-cut the execution when specific condition meets.

Such as :

    public  IEnumerator workCoroutine(){
        int i = 0;
        
        if(GLOBAL_LOCK){
          return; //will not work here.
         //we will not execute this coroutine further more, ie,shortcut it;
         //but how to ?
        }
        while( i<=1000){
           doingSomeWorkHere(i);
           i++;
           yield return null;
        }
    }

I know maybe I could do this by:
     
    public  IEnumerator workCoroutine(){
        int i = 0;
        
        if(!GLOBAL_LOCK){
           while( i<=1000){
             doingSomeWorkHere(i);
             i++;
             yield return null;
           }
        }  
    }

But I think this way may cause the code to be ugly when there are multi conditions.

So is there a way to shortcut the coroutine?


