如何在C#Windows窗体应用程序中生成无限循环

如何在C#Windows窗体应用程序中生成无限循环,c#,windows,forms,C#,Windows,Forms,我已经编写了一个C#程序,它应该循环代码来刷新日期时间信息,并将标签的文本设置为匹配。但是,当我运行它时,表单不会显示。为什么会发生这种情况,我如何使表单显示 这是我的代码: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace Clock { static class Program {

我已经编写了一个C#程序,它应该循环代码来刷新日期时间信息,并将标签的文本设置为匹配。但是,当我运行它时,表单不会显示。为什么会发生这种情况,我如何使表单显示

这是我的代码:

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

    namespace Clock
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new clock());
            }
        }
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;
名称空间时钟
{
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(新时钟());
}
}
}
忽略我的缩进(可能正确,也可能不正确,但在程序中是正确的),我做错了什么,我如何修复它

谢谢


Logan Slowik

似乎在用无限循环阻塞UI线程。您可以利用
async
来避免阻塞您的UI。。。只需在Form\u Load方法中调用此方法<代码>无穷远()


你也可以考虑使用<代码>定时器< /代码>

调试此方法的最佳方法可能是在行<代码>应用程序上运行一个断点。运行(新的时钟())<代码>,看看你是否到达它,你的问题不在提供的代码中。虽然(true){…}@series0ne,所以它是。。。通常情况下,当程序缩进不良时,情况会更加明显…@DangerZone完全同意!(我笑了!:d)
async void InfiniteLoop()
{
    while(true)
    {
        await Task.Delay(100);
        this.Text = DateTime.Now.ToString();
    }
}