1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
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;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
System.Windows.Forms.Timer m_Timer = new System.Windows.Forms.Timer();
List<DateTime> m_Horodates = new List<DateTime>();
public Form1(DateTime[] pHoroDates)
{
InitializeComponent();
m_Horodates.AddRange(pHoroDates);
m_Timer.Interval = 1000; //intervale de 1 seconde
m_Timer.Tick += new EventHandler(Traitement); //fonction à appeler
m_Timer.Start();//lancement du timer;
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
m_Timer.Stop();//arrêt du timer lorsqu'on ferme la fenêtre
}
void Traitement(object sender, EventArgs e) //ce traitement est effectué à chaque tick du timer
{
List<DateTime> dates = (from d in m_Horodates
where (d - DateTime.Now) <= TimeSpan.FromSeconds(1)
select d).ToList<DateTime>(); //retourne toutes les dates qui sont égale à la date actuelle à une seconde près (on ignore les ms)
foreach (var date in dates) //on peut avoir 0 ou n date qui correspondent dans la liste
{//petit conseil; s'il est interdit d'avoir deux fois la même date il faut mieux utiliser un HashSet (http://msdn.microsoft.com/en-us/library/bb353005.aspx)
//donc pour chaque date qui correspond on lance le traitement dans un thread secondaire
//ce qui ne bloque pas le processus de contrôle (date == date actuelle)
//ni l'interface etc...
var t = new Thread(new ParameterizedThreadStart(Action));
t.Start(date);
}
}
void Action(object pDate)
{
DateTime date = (DateTime)pDate;
//todo
}
}
} |
Partager