Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- namespace TimerFun
- {
- public partial class Form1 : Form
- {
- public event EventHandler Load;
- public Form1()
- {
- InitializeComponent();
- Timer timer1 = new Timer();
- Timer timer2 = new Timer();
- timer1.DoAfter(5, () => MessageBox.Show("5 Seconds!"));
- timer2.DoAfter(8, () => MessageBox.Show("8 Seconds!"));
- }
- }
- public static class TimerExtensions
- {
- public static Dictionary<string, Action> Timers { get; set; }
- public static void DoAfter(this Timer timer, int seconds, Action action)
- {
- if (Timers == null)
- {
- Timers = new Dictionary<string, Action>();
- }
- timer.Interval = seconds * 1000;
- timer.Tag = Guid.NewGuid().ToString();
- timer.Tick += HandleTimerTick;
- Timers.Add(timer.Tag.ToString(), action);
- timer.Enabled = true;
- timer.Start();
- }
- public static void HandleTimerTick(object sender, EventArgs e)
- {
- Timer timer = (Timer)sender;
- if (Timers.ContainsKey(timer.Tag.ToString()))
- {
- Action action = Timers[timer.Tag.ToString()];
- action();
- }
- timer.Stop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement