title: C# Manually stopping an asynchronous for-statement (typewriter effect)
body: <p>I'm making a retro-style game with <code>C#</code> <code>.NET-Framework</code>, and for dialogue I'm using a for-statement, that prints my text letter by letter (<em>like a typewriter-effect</em>):</p>
  <p><a href="https://i.stack.imgur.com/4TV3L.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4TV3L.gif" alt="enter image description here" /></a></p>
  <p>I'm working with different scenes, and I have a skip button (<em>bottom right</em>) that skips the current dialogue and passes to the next scene. My typewriter-effect automatically stops when all the text is displayed, but when I click on the skip button, it automatically skips to the next scene.</p>
  <p>I would like it, when the typewriter is still active, and if I click on the skip button, that it first shows all the text, instead of skipping to the next scene.</p>
  <p>So that it only skips to the next scene when all the text is displayed (automatically or manually).</p>
  <p>This is the (working code) that I'm using for my typewriter method (+ variables):</p>
  <pre><code>    public string FullTextBottom;
      public string CurrentTextBottom = &quot;&quot;;
      public bool IsActive;
      
      public async void TypeWriterEffectBottom()
      {
          if(this.BackgroundImage != null) // only runs on backgrounds that arent black
          {
              for(i=0; i &lt; FullTextBottom.Length + 1; i++)
              {
                  CurrentTextBottom = FullTextBottom.Substring(0, i); // updating current string with one extra letter
                  LblTextBottom.Text = CurrentTextBottom; // &quot;temporarily place string in text box&quot;
                  await Task.Delay(30); // wait for next update
                  
                  #region checks for IsActive // for debugging only!
  
                  if(i &lt; FullTextBottom.Length + 1)
                  {
                      IsActive = true;
                      Debug1.Text = &quot;IsActive = &quot; + IsActive.ToString();
                  }
                  if(CurrentTextBottom.Length == FullTextBottom.Length)
                  {
                      IsActive = false;
                      Debug1.Text = &quot;IsActive = &quot; + IsActive.ToString();
                  }
  
                  #endregion
              }
          }
  
      }
  </code></pre>
  <p>And this is the code that I <em>want to get</em> for my skip button (named <em>Pb_FastForward</em>):</p>
  <pre><code>    private void PbFastForward_Click(object sender, EventArgs e)
      {
          if( //typewriter is active)
          {
               //print all text into the textbox
          }
          
          else if( //all text is printed)
          {
               // skip to the next scene
          }
      }   
  </code></pre>
  <p>But I don't know how to formulate the 2nd part of code. I've tried many different approaches, like using counters that increase on a buttonclick (and using that to check in an if-statement), and many different types of if-statements to see if the typewriter is still active or not, but I haven't got anything to work yet.</p>
  <h2>Edit</h2>
  <p>This is the sequence in which different components need to be loaded (on button click), which is related to the way different variables are updated:</p>
  <ol>
  <li><code>Gamestate_Cycle()</code> --&gt; called for loading new scene.</li>
  <li><code>FullTextBottom = LblTextBottom.Text</code> --&gt; called to refresh variables for typewriter.</li>
  <li><code>TypeWriterEffectBottom()</code> --&gt; called to perform typewriter effect.</li>
  </ol>