C# 如何使用计时器代替while循环?

C# 如何使用计时器代替while循环?,c#,timer,C#,Timer,目前,我正在使用while(true)循环来实现这一点。我对计时器不太熟悉。有人能告诉我如何将此转换为与计时器一起工作吗 string lyricspath = @"c:\lyrics.txt"; TextReader reader = new StreamReader(lyricspath); int start = 0; string[] read = File.ReadAllLines(lyricspath); string join = String.Join(" ", read); i

目前,我正在使用while(true)循环来实现这一点。我对计时器不太熟悉。有人能告诉我如何将此转换为与计时器一起工作吗

string lyricspath = @"c:\lyrics.txt";
TextReader reader = new StreamReader(lyricspath);
int start = 0;
string[] read = File.ReadAllLines(lyricspath);
string join = String.Join(" ", read);
int number = join.Length;
while (true)
{
    Application.DoEvents();
    Thread.Sleep(200);
    start++;
    string str = join.Substring(start, 15);
    byte[] bytes = Encoding.BigEndianUnicode.GetBytes(str);
    label9.Text = str;
    if (start == number - 15)
    {
        start = 0;
    }
}

这是每200毫秒运行一次的,但是在这里提出问题之前尝试谷歌是一个很好的建议

using Timer = System.Windows.Forms.Timer;

private static readonly Timer MyTimer = new Timer();
...
MyTimer.Tick += MyTimerTask;
MyTimer.Interval = 200; // ms
MyTimer.Enabled = true;
...
private void MyTimerTask(Object o, EventArgs ea)
{
    ...
}

基本上,计时器只是坐在那里计数,每X毫秒它“滴答”一次;它会引发一个事件,您可以使用一个方法订阅该事件,该方法每X毫秒执行一次您想要执行的操作

首先,循环内部需要的所有变量(来自循环外部)都需要具有“实例范围”;它们必须是当前具有此方法的对象的一部分,而不是像现在这样的“局部”变量

然后,您当前的方法将需要执行while循环之前的所有步骤,设置我提到的“实例”变量,然后创建并启动计时器。NET中有几个计时器;最有用的两个可能是System.Windows.Forms.Timer或System.Threading.Timer。这个计时器需要有一个句柄,当它“滴答”时,它应该调用这个方法,并且应该被告知“滴答”的频率

最后,while循环中的所有代码,除了对Application.DoEvents()和Thread.Sleep()的调用外,都应该放在计时器“滴答”时运行的方法中

这样的事情应该行得通:

private string[] join;
private int number;
private int start;
private Timer lyricsTimer;

private void StartShowingLyrics()
{
   string lyricspath = @"c:\lyrics.txt";
   TextReader reader = new StreamReader(lyricspath);
   start = 0;
   string[] read = File.ReadAllLines(lyricspath);
   join = String.Join(" ", read);
   number = join.Length;

   lyricsTimer = new System.Windows.Forms.Timer();
   lyricsTimer.Tick += ShowSingleLine;
   lyricsTimer.Interval = 300;
   lyricsTimer.Enabled = true;
}


private void ShowSingleLine(object sender, EventArgs args)
{
    start++;
    string str = join.Substring(start, 15);
    byte[] bytes = Encoding.BigEndianUnicode.GetBytes(str);
    label9.Text = str;
    if (start == number - 15)
    {
        start = 0;
    }
}

为什么要用定时器?我想这是因为你想让应用程序在这么长的运行时间内保持响应。如果是这样,则使用相同类型的代码,但在BackgroundWorker中进行排序


此外,如果你确实想使用计时器,请注意使用哪一个;系统计时器在与应用程序热处理窗体使用的线程不同的线程中调用其事件。窗体线程中窗体事件中的计时器。您可能需要调用()计时器回调中更改标签的操作。

只需定义一个新计时器:

        Timer timer1 = new Timer();
        timer1.Interval = 1; // Change it to any interval you need.
        timer1.Tick += new System.EventHandler(this.timer1_Tick);
        timer1.Start();
然后定义一个方法,该方法将在每个计时器计时(每[Interval]毫秒)调用一次:


*记住在方法之外定义变量,这样您就可以在timer1\u Tick方法中访问它们。

您看过如何使用计时器了吗。。你看了吗。。有成千上万的例子…他是对的。谷歌在这类简单的事情上确实是你的朋友。
    private void timer1_Tick(object sender, EventArgs e)
    {
        Application.DoEvents();
        Thread.Sleep(200);
        start++;
        string str = join.Substring(start, 15);
        byte[] bytes = Encoding.BigEndianUnicode.GetBytes(str);
        label9.Text = str;
        if (start == number - 15)
        {
            start = 0;
        }
    }