title: Looping Forever in a Windows Forms Application
body: <p>I am using Visual C# sand I'm using a Windows Form rather than a console application. Therefore I'm not working in Main (), but rather in the Form File. I'm also very new to C# so sorry if some of my questions are stupid.</p>
  <p>What I basically need to do is when my program starts up I need it to keep looping forever. Where would I put this code since I don't have a Main ()? Do I put it in the function that has InitializeComponent() in it? I need the loop to start right after the program starts. However, I have some variables that I need declared first before the loop. So basically I need the variables to be declared and then the infinite loop to start. The variables are global.</p>
  <p>Secondly, I need the infinite loop to break when the user presses a button. How would I do this? I was thinking something among the lines of:</p>
  <pre><code>while (buttonIsPressed == false)
  {
     //do whatever I need to do
  }
  </code></pre>
  <p>However, I realized the button's function will never be called since I am stuck in that loop. I can't set the variable from the button's function if I never reach the button's function due to being in an infinite loop. Any ideas? I was thinking about threads but I have absolutely no experience with threads so am a bit reluctant to try it out.</p>
  <hr />
  <h3>Additional, from comments:</h3>
  <blockquote>
  <p>A chat application. When the program starts I need it to keep
  listening. However, when the user clicks &quot;connect&quot; it stops listening
  and instead initiates a connection</p>
  <p>I am creating a chat client. So basically when my program starts up, I
  need it to keep listening. However, when the user clicks &quot;connect&quot; it
  needs to stop listening and instead initiate a connection</p>
  </blockquote>