C# &引用;拖放以形成;visual studio express 2013中的计时器没有';行不通

C# &引用;拖放以形成;visual studio express 2013中的计时器没有';行不通,c#,.net,winforms,C#,.net,Winforms,我不知道为什么这个代码不起作用 计时器只是用来冻结一切。据我所知,计时器被发送到一个新创建的线程。我试过锁定蜱虫,但没有成功 namespace WindowsFormsApplication1 { public partial class Form1 : Form { public int CountUp = 0; public Form1() { InitializeComponent();

我不知道为什么这个代码不起作用

计时器只是用来冻结一切。据我所知,计时器被发送到一个新创建的线程。我试过锁定蜱虫,但没有成功

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public int CountUp = 0;
        public Form1()
        {
            InitializeComponent();
            label1.Text = "Click To Start Timer";
            timer1.Enabled = false;
            timer1.Stop();
            timer1.Interval = 1000;

        }

        private void button1_Click(object sender, EventArgs e)
        {

            bool Toggled = false;
            if (!Toggled)
            {
                timer1.Enabled = true;
                Toggled = true;
            }
            else
            {
                timer1.Enabled = false;
                Toggled = false;
                label1.Text = "Timer Stopped";
                CountUp = 0;
            }
            while(Toggled){
                label1.Text = "Timer1 Running";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //lock (label2.Text) <- froze the program immediately
            //{
            //    CountUp++;
            //    string CurrentTime = CountUp.ToString();
            //    label2.Text = CurrentTime;
            //}
            CountUp++;
            string CurrentTime = CountUp.ToString();
            label2.Text = CurrentTime;
            //CurrentTime = timer1.ToString() = Garbage & freeze.
        }

    }
}
命名空间窗口窗体应用程序1
{
公共部分类Form1:Form
{
公共整数倒计时=0;
公共表格1()
{
初始化组件();
label1.Text=“单击以启动计时器”;
timer1.Enabled=false;
timer1.Stop();
计时器1。间隔=1000;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
bool-Toggled=false;
如果(!已切换)
{
timer1.Enabled=true;
切换为true;
}
其他的
{
timer1.Enabled=false;
切换=假;
label1.Text=“计时器已停止”;
倒计时=0;
}
while(切换){
label1.Text=“Timer1正在运行”;
}
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
//lock(label2.Text)while(Toggled){..}
代码将冻结接口并挂起程序,因为它会阻止UI线程。但是,依赖于正在处理的UI事件队列的不会由于阻塞循环而触发,因此,标志变量永远不会被“重置”

虽然可以使用其中一个线程计时器,但它仍然会导致用户界面不响应。解决方案是设计[WinForm]程序以对事件做出反应,例如计时器何时启动和何时停止1


1实际上没有启动/停止事件本身,但在计时器由代码启动或停止时执行操作。例如

private void button1_Click(object sender, EventArgs e)
{
    if (!timer1.Enabled) {
        CountUp = 0;
        timer1.Enabled = true;
        label1.Text = "Timer1 Running";
    } else {
        timer1.Enabled = false;
        label1.Text = "Timer1 Stopped (Manual)";
    }
}

// Later on, when a tick occurs and the timer should end..
// The timer callback runs on the UI thread it was created on.
private void timer1_Tick(object sender, EventArgs e)
{
    CountUp++;
    if (CountUp > 10) {
        timer1.Enabled = false;
        label1.Text = "Timer1 Stopped (Time up)";
    }
}

根本不需要使用
lock
,因为所有代码都应该在UI线程上运行。

您是否尝试过单步执行程序?程序冻结在哪里?您的计时器没有发送到新创建的线程。它的事件在主线程上执行。太好了!谢谢。bool类/类型应该有一个正确的反转另外,我发现else语句是使其跳转到正确代码的关键。第1课。使用进程状态进行切换。第2课。在希望有continue语句的地方使用“else”。