如何使用计时器使用线程进行操作? [英] how to make an action with a thread using a timer?

查看:140
本文介绍了如何使用计时器使用线程进行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有这些代码行,



所以我想每隔一段时间反复调用函数EventoPeriodico以保存一些信息,但我也有另一个我想在不同线程中调用的函数,每个线程都有自己不同的时间。可以使用区分线程和计时器调用每个函数。



I have these code lines in C#,

so I want to call the function "EventoPeriodico" repeatedly every certain time to save some information, but I also have another functions that I would like to call inside differents threads, and each thread has its own different time. Every function can be call with distincts threads and timers.

hilos = new Thread[Count];

hilos[i] = new Thread(new ThreadStart(EventoPeriodico));
hilos[i].Start();





这是一个例子,但如果我有多个事件怎么办? /行动?因为用户决定他想要多少事件或动作。如何避免为每个事件/动作编写每个条件?





This is an example, But what if I have more than one Event/action? Because the user decides how many Events or actions he wants. How can I avoid writing every condition for each Event/action?

private void button1_Click(object sender, EventArgs e){

       for (int i = 0; i <= index-1; i++) { 
        timers[i] = new Timer() { Interval = Intervalos[i] }; 
        timers[i].Start(); timers[i].Enabled = true; 
        timers[i].Tag = i; 
        timers[i].Tick += new EventHandler(timer1_Tick); 
        }
     } 

 private void timer1_Tick(object sender, EventArgs e) {

      Timer timer1 = sender as Timer; 
      int index = (int)timer1.Tag; 
      if (index == 0) { 
      label1.Text = cont1+""; 
      cont1++; 
      } 
      if (index == 1) { 
      label2.Text = cont2 + ""; 
      cont2++; 
      } 
   } 

推荐答案

这篇关于如何使用计时器使用线程进行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆