-1

I have started a little app for fun and it needs to make "form1" (See code) keep opening a new window forever. It does this, however it only does the loop after the previous window is closed. So basically it needs to keep opening up additional windows forever at the same time without the user needing to close the window that is already open. Picture of current code

Code of the whole file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PeppaPig
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
        loop:
            Application.Run(new Form1());
            goto loop;
        }
    }
}

Thanks! Any help is appreciated!

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • 1
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Nov 21 '18 at 08:49
  • 1
    And don't post images of code, copy-paste the code itself instead. – Some programmer dude Nov 21 '18 at 08:49
  • On a note related to the code, don't use labels and `goto` for loops. In fact, you should really not use `goto` *at all*. – Some programmer dude Nov 21 '18 at 08:50
  • 2
    Possible duplicate of [Which is the correct C# infinite loop, for (;;) or while (true)?](https://stackoverflow.com/questions/1401159/which-is-the-correct-c-sharp-infinite-loop-for-or-while-true) – mjwills Nov 21 '18 at 08:55
  • @Someprogrammerdude Hey! Thanks for your help. What should I use instead of goto? – Riley Cogdell-Baird Nov 21 '18 at 08:55
  • 1
    `while (true)` is usually a common way. Besides, having a program which you can not exit is usually not very liked among users. – Some programmer dude Nov 21 '18 at 08:58
  • So you want to run a tight loop that opens new windows so fast that nobody can interact with any of them in any meaningful way? What useful purpose would that achieve? (Other than crashing out once you exhaust handles or your address space) – Damien_The_Unbeliever Nov 21 '18 at 09:03

2 Answers2

0

Your code waits until...

Application.Run(new Form1());

... has finished. To achieve the behaviour you want, you should create an instance of Form1 and call Show() of it.

To achieve an infinit loop there are different ways. Some examples are

while(true)
{
   //do something
}

or

for(;;)
{
    // do something
}

or like you already did

:loop
// do something
goto loop;

So here is an example how to get the behaviour you want:

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        while (true)
        {
            var newForm = new Form1();
            newForm.Show();
        }
    }
Rich
  • 98
  • 6
-1

You should change your main function to something like this.

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());

       while (true)
       {
            Form1 frm = new Form1();
            frm.Show();
       }
    }
Sasan
  • 149
  • 15