简体   繁体   English

如何在一个标签上打印多个数字

[英]how do i print more than one number to a single label

After watching Futurama start to finish for the third or forth time I decided I wanted to try and program up the random number count down generator from episode "A Tale of Two Santas". 观看了Futurama第三次或第四次结束后,我决定我想尝试对第二集“两个圣诞老人的故事”中的随机数倒计时生成器进行编程。 When I first started this project I only started with displaying one single number at a time, during this step the program appeared to run perfectly. 当我第一次启动该项目时,我仅一次只显示一个数字,在此步骤中,程序似乎运行良好。 Then I moved onto trying to make my display match the one in the episode with a second delay between each number. 然后,我着手尝试使我的显示与剧集中的显示匹配,并且每个数字之间都有第二个延迟。 After I added code to do this my program doesn't display any number until it hits zero and then closes the program. 添加代码以执行此操作之后,我的程序将显示为零,然后关闭该程序,之后才会显示任何数字。 Instead When I first click my button, it displays "label" for the first second then it cuts it back to "lab" or "la" and half of the letter be until it hits zero. 相反,当我第一次单击我的按钮时,它在第一秒钟显示“标签”,然后将其剪切回“实验室”或“ la”,并且一半字母变为,直到其变为零。 I Have no clues as to why this is happening and would appreciate any input into why this is happening. 我不知道为什么会发生这种情况,不知道为什么会发生这种情况。 My code is below. 我的代码如下。

namespace FuturomaRandomNumberCountDown
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool keepRunning = true; //used for loop kickout
        private void start_Click(object sender, EventArgs e)
        {
            while (keepRunning == true)
            {
                Random random = new Random((int)DateTime.Now.Ticks);
                int randomNum = random.Next(0, 99);

                display.Text = Convert.ToString(randomNum);/display number
                Thread.Sleep(1000);//delay

                if(randomNum==0)//check for zero
               { keepRunning = false; }//to kick out of the while loop
            }
            Application.Exit();
        }
    }
}

It has to do with threads and what Thread.Sleep actually does. 它与线程以及Thread.Sleep实际作用有关。 When you are running your program, all of the GUI-related code runs on the same thread. 当您运行程序时,所有与GUI相关的代码都在同一线程上运行。 This is the same thread that runs all your event code, so when you do some processing in event code, your GUI isn't going to update until that code returns. 这是运行所有事件代码的线程,因此,当您对事件代码进行一些处理时,除非该代码返回,否则GUI不会更新。

In your case, you are looping through every value you want to display within the same while loop. 在您的情况下,您要遍历要在同一while循环中显示的每个值。 This is hogging the main thread of your program, and it's not giving the GUI a chance to update in between every change. 这占用了程序的主线程,并且没有给GUI提供在每次更改之间进行更新的机会。 Additionally, when you call Thread.Sleep , that pauses whatever is the current thread, which in this case is your main thread. 此外,当您调用Thread.Sleep ,它将暂停当前线程,在此情况下为您的主线程。 That has the effect of pausing the entire program. 这会暂停整个程序。

In order to do what you want to do, you're going to have to one of two things. 为了做您想做的事,您将不得不做以下两件事之一。 Either use multithreading to put your random number generator on a different thread , or use a Timer object to handle your refreshing . 使用多线程将随机数生成器放在其他线程上 ,或者使用Timer对象处理刷新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM