先上代码:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Timers;namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private TimeSpan ts= new TimeSpan();
        
        public Form1()
        {            InitializeComponent();
        }
        int a, b, c;
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            a = int.Parse(textBox2.Text);
            b = int.Parse(textBox3.Text);
            c = int.Parse(textBox4.Text);
            ts = new TimeSpan(a,b,c);
        }
        private void timer1_Tick(object sender, EventArgs e)
           {               
               string str = ts.Hours.ToString() + ":" + ts.Minutes.ToString() + ":" + ts.Seconds.ToString();
               textBox1.Text = str;
               ts=ts.Subtract(new TimeSpan(0,0,1));
               if (ts.TotalSeconds < 0.0)
            {
                timer1.Enabled = false;
            }
              
          }        private void textBox1_TextChanged(object sender, EventArgs e)
        {        }    }
}
代码中 我们有一个按钮 四个textbox 和一个timer控件 就是一个小的计时器
在前3个按钮输入时间 点 button1就能实现倒数计时,
但我想在一个界面弄几个这样的计时器,
我想在新建一个按钮控件 点一下 就可以新建一个计时器实现大量的不同对象的计时
高手帮帮忙  

解决方案 »

  1.   

    一句话:
    new Thread((ThreadStart)delegate{Application.Run(new Form1());}).Start();
      

  2.   

    就是 可不可以弄个数据库进去 然后 ·XXOO  插个表的控件显示出来·
      

  3.   

    你是想一个窗体里面实现,那就是多线程问题,这是我刚写的一个简单的多线程测试
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;namespace ThreadTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            Thread t1, t2;
            private void button1_Click(object sender, EventArgs e)
            {
                t1 = new Thread(new ThreadStart(Test1));
                t2 = new Thread(new ThreadStart(Test2));
                t2.Start();
              t1.Start();
            }
            private void Test1()
            {
                Control.CheckForIllegalCrossThreadCalls = false;
                
                while (true)
                {
                    label1.Text = DateTime.Now.ToString();
                    Thread.Sleep(1000);
                 
                 
                  
                }
            }
            private void Test2()
            {
                Control.CheckForIllegalCrossThreadCalls = false;            while (true)
                {
                    label2.Text = DateTime.Now.ToString();
                    Thread.Sleep(1000);            }
            }        private void button3_Click(object sender, EventArgs e)
            {
                if (t1 != null)
                {
                    t1.Suspend();
                }
            }        private void button4_Click(object sender, EventArgs e)
            {
                if (t1 != null)
                {
                    t1.Resume();
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                if (t2 != null)
                {
                    t2.Suspend();
                }
            }        private void button5_Click(object sender, EventArgs e)
            {
                if (t2 != null)
                {
                    t2.Resume();
                }
            }
            
        }
    }
    当然他会提示创建窗口句柄出错,之类的,还会有警告,这只是测试给你看下是怎么来实现的,这不是最优方案,如果你没加Control.CheckForIllegalCrossThreadCalls = false;会运行不了
    是因为.NET 2.0以后加强了安全机制,不允许WF直接跨线程访问控件,如果你是03版确实这样是可以的