rul3z
rul3z

Reputation: 183

C# winform - Raise a personal event in Timer_tick event

I have create a small class CountDown , that contains a timer.

Class is very simple : receive time target, and start a CountDown with a timer.

When target is reached, my personal event fire:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CountDownWithEvent
{
    public delegate void countDownFinishEventHandler(Object sender, EventArgs e);
    class CountDown
    {
        private DateTime _target;
        private System.Windows.Forms.Timer _timer;
        System.TimeSpan _timeMissing;


        public event countDownFinishEventHandler CountDownFinish;

        public CountDown(DateTime targetTime)
        {
            this._target = targetTime;
            this._timer = new System.Windows.Forms.Timer();
            this._timeMissing = new TimeSpan();
        }

        public DateTime TargetTime
        {
            get;
        }

        public TimeSpan timeMissing
        {
            get;
        }

        public void CountDownStart()
        {
            _timer.Interval = 1000;
            _timer.Tick += new EventHandler(timer_tick);
            _timer.Start();
        }

        protected virtual void timer_tick(Object sender, EventArgs e)
        {
            //if (_timer.Tick != null)
            //{
            //}
            System.DateTime now = System.DateTime.Now;
            _timeMissing = _target.Subtract(now);
            if (!(timeMissing.TotalSeconds > 0))
            {
                _timer.Stop();
                if(CountDownFinish != null)
                {
                    EventArgs b = new EventArgs();
                    CountDownFinish(this, b);
                }
            }
        }


    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CountDownWithEvent
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CountDown CountDown = new CountDown(new DateTime(2016, 05, 27, 14, 48, 00));
            CountDown.CountDownFinish += new countDownFinishEventHandler(onCountDown);
            CountDown.CountDownStart();
        }

        private void onCountDown(Object sender, EventArgs e)
        {
            MessageBox.Show("time expired! ");
        }
    }
}

I use EventArgs, rather than write a derived class , because I don't need any special information of the event ( to understand, parameter e)

now I'm in a situation a bit unusual :

protected virtual void timer_tick(Object sender, EventArgs e)
    {
        //if (_timer.Tick != null)
        //{
        //}
        System.DateTime now = System.DateTime.Now;
        _timeMissing = _target.Subtract(now);
        if (!(timeMissing.TotalSeconds > 0))
        {
            _timer.Stop();
            if(CountDownFinish != null)
            {
                EventArgs b = new EventArgs();
                CountDownFinish(this, b);
            }
        }
    }

When i call CountdownFinish(this,e); the e parameter refer an timer_tick It isn t consistent pass EventArgs timer so I don t know how to behave ???

In fact I have instantiated new EventArgs b

            EventArgs b = new EventArgs();
            CountDownFinish(this, b);

but I don't know if this is the right path

Now i'm another problem :

I want to see in label the time left to goal. And refresh it to any timer_tick. whereas I want to keep separated the timer logic of the program graphic.. how I can do it?

Many thanks in advance for the understanding and the help! (sorry for bad english, isn't my language :) )

Upvotes: 1

Views: 595

Answers (1)

rul3z
rul3z

Reputation: 183

@Reza Aghaei

ok, this is my solution .. What do you think ?!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EventsTry
{
    public delegate void countDownFinishEventHandler(Object sender, EventArgs e);
    public delegate void TimeLeftChangedEventHandler(Object sender, TimeLeftDateEventArgs e);
    class CountDown
    {
        private DateTime _target;
        private System.Windows.Forms.Timer _timer;
        System.TimeSpan _timeMissing;

        public event countDownFinishEventHandler CountDownFinish;
        public event TimeLeftChangedEventHandler TimeLeftChanged;

        public CountDown(DateTime targetTime)
        {
            this._target = targetTime;
            this._timer = new System.Windows.Forms.Timer();
            this._timeMissing = new TimeSpan();
        }

        public DateTime TargetTime
        {
            get { return this._target; }
        }

        public TimeSpan timeMissing
        {
            get { return this._timeMissing; }
        }

        public void CountDownStart()
        {
            _timer.Interval = 1000;
            _timer.Tick += new EventHandler(timer_tick);
            _timer.Start();
        }

        protected virtual void timer_tick(Object sender, EventArgs e)
        {
            System.DateTime now = System.DateTime.Now;
            _timeMissing = _target.Subtract(now);


            if (!(timeMissing.TotalSeconds > 0))
            {
                _timer.Stop();


                if (CountDownFinish != null)
                {
                    CountDownFinish(this, EventArgs.Empty);
                }
            }
            else
            {
                if (TimeLeftChanged != null)
                {
                    TimeLeftChanged(this, new TimeLeftDateEventArgs(timeMissing));
                }
            }
        }

    }

    public class TimeLeftDateEventArgs : EventArgs
    {
        private int _hours;
        private int _minutes;
        private int _seconds;

        public TimeLeftDateEventArgs(TimeSpan timespan)
        {
            _hours = timespan.Hours;
            _minutes = timespan.Minutes;
            _seconds = timespan.Seconds;

        }

        public int Hours
        { get { return this._hours; } }

        public int Minutes
        { get { return this._minutes; } }

        public int Seconds
        { get { return this._seconds; } }

    }


}

form class

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EventsTry
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CountDown CountDown = new CountDown(new DateTime(2016,06,01,11,35,00));
            CountDown.CountDownFinish += new countDownFinishEventHandler(onCountDown);
            CountDown.TimeLeftChanged += new TimeLeftChangedEventHandler(onTimeLeft);
            CountDown.CountDownStart();
        }

        private void onCountDown(Object sender, EventArgs e)
        {
            MessageBox.Show("time expired! ");
        }

        private void onTimeLeft(Object sender, TimeLeftDateEventArgs e)
        {
            label1.Text = e.Hours + ":" + e.Minutes + ":" + e.Seconds;
        }
    }
}

I know that delegate countDownFinishEventHandler it's equal to EventHandler. I haven t changed out of laziness.

I wanted to use an inner class to TimeLeftDateEventArgs but public delegate is extern to class CountDown then TimeLeftDateEventArgs isn't not accessible to delegate.

2- Create TimeLeft property which shows remaining time to finish and decrease it in the timer tick event.

you meant this solution ? or other way !?

Upvotes: 1

Related Questions