如何在没有用户操作的情况下无限期地循环 运行

How to make loop run indefinitely without user action

我为了好玩开始了一个小应用程序,它需要让 "form1"(见代码)永远打开一个新的 window。它会这样做,但是它只会在前一个 window 关闭后才执行循环。所以基本上 它需要同时打开额外的 windows 永远 而用户不需要关闭已经打开的 window。 Picture of current code

整个文件的代码:

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;
        }
    }
}

谢谢!感谢您的帮助!

您的代码等到...

Application.Run(new Form1());

...已完成。要实现您想要的行为,您应该创建 Form1 的实例并调用它的 Show() 。

要实现无限循环,有不同的方法。 一些例子是

while(true)
{
   //do something
}

for(;;)
{
    // do something
}

或者像你已经做过的那样

:loop
// do something
goto loop;

下面是一个如何获得您想要的行为的示例:

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

        while (true)
        {
            var newForm = new Form1();
            newForm.Show();
        }
    }

你应该把你的主要功能改成这样。

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

       while (true)
       {
            Form1 frm = new Form1();
            frm.Show();
       }
    }