一个timer中可以同时运行多个线程吗?
怎么才能访问线程中的控件?现在有一个timer,每隔1分钟查询出A表的数据后插入到另一个数据库的B表中
一个有4张表,我想在一个timer中用4个线程,但是不能对控件进行赋值
请问怎么解决比较好?才能对控件赋值?代码:
 public partial class SystemSet : Form
    {
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();        int timeSum = 0;
        int upCount = 0;
        int thread1Count = 0;
        int thread2Count = 0;
        int thread3Count = 0;
        int thread4Count = 0;
        int timeSpan = 60000;//毫秒
        DateTime timeStart = new DateTime();
        DateTime timeCurrent = new DateTime();        public SystemSet()
        {
            InitializeComponent();            string strTimeSpan = Common.Config.GetTimeSpan();
            if (!string.IsNullOrEmpty(strTimeSpan))
            {
                timeSpan = int.Parse(strTimeSpan);
            }
            timer_Set();
            timer_Start();            Rock.Utils.AutoStartup.SetAutoRun(Application.ExecutablePath, true);
        }        private void timer_Event(object sender, EventArgs args)
        {
            try
            {
                insert2Center();
                timeSum += timeSpan / 1000;//转换成秒                TimeSpan ts = TimeSpan.FromMilliseconds(timeSum * 1000);
                string strTimeSpan = string.Format("{0}天{1}时{2}分{3}秒", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
                LB_RunTime.Text = strTimeSpan; //显示到lable    
                LB_CurrentTime.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                // this.Invoke(new UpdateUICallBack(Update_UIEvent));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void insert2Center()
        {
            try
            {
                Thread thread1 = new Thread(new ThreadStart(thread1_Run));
                thread1.Start();
                Thread thread2 = new Thread(new ThreadStart(thread2_Run));
                thread2.Start();
                Thread thread3 = new Thread(new ThreadStart(thread3_Run));
                thread3.Start();
                Thread thread4 = new Thread(new ThreadStart(thread4_Run));
                thread4.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }        }
        public void thread1_Run()
        {
            try
            {
                string strResult1 = WebService.XianKeShengXian.SuperMarBaseInfoWebService.Insert();
                thread1Count++;
                LB_BaseInfo_Count.Text = thread1Count.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void thread2_Run()
        {
//对表2的插入
        }
        public void thread3_Run()
        {
//对表3的插入
        }
        public void thread4_Run()
        {
//对表4的插入
        }
        public void timer_Set()
        {
            timer.Interval = timeSpan;
            timer.Tick += new EventHandler(timer_Event);
        }
        public void timer_Start()
        {
            try
            {
                timer.Start();
                timeStart = DateTime.Now;
                timeCurrent = timeStart;
                LB_StartTime.Text = timeStart.ToString("yyyy-MM-dd hh:mm:ss");
                LB_CurrentTime.Text = timeCurrent.ToString("yyyy-MM-dd hh:mm:ss");
                timeSum = 0;
                string strTimeSpan = string.Format("{0}天{1}时{2}分{3}秒", 0, 0, 0, 0);
                LB_RunTime.Text = strTimeSpan;
                LB_Tip.Text = "当前系统正在运行";                thread1Count = 0;
                thread2Count = 0;
                thread3Count = 0;
                thread4Count = 0;
                LB_BaseInfo.Text = "运行中";
                LB_SupplierBaseInfo.Text = "运行中";
                LB_MeatEntryInfo.Text = "运行中";
                LB_VegEntryInfo.Text = "运行中";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void timer_Stop()
        {
            try
            {
                timer.Stop();
                LB_Tip.Text = "当前系统已停止运行";                LB_BaseInfo.Text = "已停止";
                LB_SupplierBaseInfo.Text = "已停止";
                LB_MeatEntryInfo.Text = "已停止";
                LB_VegEntryInfo.Text = "已停止";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }        }
        private void Btn_Start_Click(object sender, EventArgs e)
        {
 .....
        }        private void Btn_Stop_Click(object sender, EventArgs e)
        {
......
        }
    }

解决方案 »

  1.   

    invoke代理public delegate void setLabelTextDelegate();//定义委托
    private setLabelTextDelegate setLabelText;//定义委托变量
    public Form1()
     {
           InitializeComponent();
           this.setLabelText = this.SetLableText
     }
    private void ThreadMethod()
     {
           this.label1.Invoke(this.setLabelText);//setLabelText为上面定义的委托变量
     }
    System.Threading.Thread setLabelTextThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.SetLableText));//这个放到你要访问控件的方法或者事件里面
      

  2.   

    Timer和线程有什么关系?这个问题如同问一个闹钟可以使用四辆自行车么?
      

  3.   

    Control.Invoke
    BeginInvoke+EndInvoke+CallBack
      

  4.   

    一个 timer 自身就是一个独立线程?
      

  5.   

    你把system.window.form.timer换成system.timer.timer试试看
    system.window.form.timer是基于UI的,是单线程的
    你可以在一个system.timer.timer里面创建多个线程,在线程里面调用invoke
    如:
    private delegate void SetLabelTextHandler(Label lableInvoke, string text);
            private void SetLabelText(Label lableInvoke, string text)
            {
                try
                {
                    // 如果返回 True ,则访问控件的线程不是创建控件的线程 
                    if (lableInvoke.InvokeRequired)
                    {
                        SetLabelTextHandler handler = new SetLabelTextHandler(SetLabelText);
                        //if (!this.IsDisposed)//注意:要判断窗体是否已销毁,否则会出现,异常提示:“在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke”
                        if (this.IsHandleCreated)
                        {
                            this.BeginInvoke(handler, new object[] { lableInvoke, text });
                        }
                    }
                    else
                    {
                        lableInvoke.Text = text;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }