Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7002137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:53:26+00:00 2026-05-27T20:53:26+00:00

I am using Visual C# sand I’m using a Windows Form rather than a

  • 0

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.

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.

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:

while (buttonIsPressed == false)
{
   //do whatever I need to do
}

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.


Additional, from comments:

A chat application. When the program starts I need it to keep
listening. However, when the user clicks "connect" it stops listening
and instead initiates a connection

I am creating a chat client. So basically when my program starts up, I
need it to keep listening. However, when the user clicks "connect" it
needs to stop listening and instead initiate a connection

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:53:27+00:00Added an answer on May 27, 2026 at 8:53 pm

    That’s a natural question for someone who’s coming from a (completely) different background.

    Programming Windows Forms applications is event driven. When a Windows Forms application starts, a form is loaded (check Program.cs file) and there is a Main loop that is hidden from you to concentrate on the important things in your program.

    You don’t need to put anything in the Main loop to respond to an event (like a button clicked event). You just handle the button clicked event by creating an event handler for the Click event of the Button). You can use the designer or do that manually in the code.

    When you create a method to handle an event (and as such the method is called an event handler) it is called automatically when the event is raised/triggered. For example a method handler for the Click event for the Button on your form would be called when the user clicked the button.

    This MSDN topic contains all the information you need: Creating Event Handlers in Windows Forms. If you need more clarification, please ask! 🙂

    UPDATE: Create an event handler just like above and also create a loop in the Form_Loaded event handler. Be sure to call Application.DoEvents(); inside the loop. This way the event handler for the button click can be handled (in the handler add code to modify a boolean that would make the loop’s condition false to cancel the loop).

    UPDATE 2:
    To make this answer more complete, I mention that you should also consider running the loop on a new thread, not the UI one (and therefore avoid needing to use DoEvents, which has its negatives as my peers pointed out). The following example shows how to create a Thread and cancel it on a Button Click:

        System.Threading.Thread t;
        private void Form1_Load(object sender, EventArgs e)
        {
            //create and start a new thread in the load event.
            //passing it a method to be run on the new thread.
            t = new System.Threading.Thread(DoThisAllTheTime);
            t.Start();
        }
    
        public void DoThisAllTheTime()
        {
            while (true)
            {
                //you need to use Invoke because the new thread can't access the UI elements directly
                MethodInvoker mi = delegate() { this.Text = DateTime.Now.ToString(); };
                this.Invoke(mi);
            }
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            //stop the thread.
            t.Suspend();
        } 
    

    Finally, consider using a BackgroundWorker which encapsulates creating and managing Threads for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Visual Studio 2008 and VB.Net: I have a working web app that uses
Using Visual Source Safe with IIS integration (the working dir is the IIS site)
Using Visual C++ 2008 Express Edition. I'm linking my application with an import library
Using Visual Studio 2010 beta, when I run my application within the IDE for
Using Visual Studio 2010, I developed a WCF service hosted on a web application
Most of my career I've been in windows environments using IntelliJ/Eclipse/Visual Studio. I've recently
Using Visual Studio 2010, .NET 4 as a Console App. With a breakpoint on
Using Visual Studio 2008 (C#) I have to make a working clock (digital) with
Using Visual Studio 2005 - C# 2.0, System.Net.WebClient.UploadData(Uri address, byte[] data) Windows Server 2003
Using visual studio 2008 SP1, This line: LINK : debug\XXXXX.exe not found or not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.