0

I'm totally new to Events and delegates in C#. I want to handle a data read event in one class (ex. program) and port reading is done in another class(ex. transfer). I know how to do it with delegates alone,but don't know how to do it in events.

Could you give me a simple sample. Thanks.

2
  • 5
    I'm pretty sure Microsoft has samples for this. Commented Sep 29, 2011 at 12:30
  • 2
    Hi - normaly you just "add" the handler to the events like Button.Click += OnButtonClickedHandler; - BUT what do you men with (ex. program)? Do you want to handle events across processes/programms? Commented Sep 29, 2011 at 12:31

2 Answers 2

3

look at this example

    public class TimerManager : INotifyPropertyChanged
    {
    private readonly DispatcherTimer dispatcherTimer;
    private TimeSpan durationTimeSpan;
    private string durationTime = "00:00:00";
    private DateTime startTime;
    private bool isStopped = true;
    readonly TimeSpan timeInterval = new TimeSpan(0, 0, 1);
    public event EventHandler Stopped;

    public TimerManager()
    {
        durationTimeSpan = new TimeSpan(0, 0, 0);
        durationTime = durationTimeSpan.ToString();
        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += DispatcherTimerTick;
        dispatcherTimer.Interval = timeInterval;
        dispatcherTimer.IsEnabled = false;
        DefaultStopTime = new TimeSpan(17, 30, 0);

    }

    public TimerManager(TimeSpan defaultStopTime)
        : this()
    {
        DefaultStopTime = defaultStopTime;
    }
    #region Properties

    public TimeSpan ElapsedTime
    {
        get { return durationTimeSpan; }
    }
    public string DurationTime
    {
        get { return durationTime; }
        set
        {
            durationTime = value;
            OnPropertyChanged("DurationTime");
        }
    }

    public DateTime StartTime
    {
        get { return startTime; }

    }
    public bool IsTimerStopped
    {
        get
        {
            return isStopped;
        }
        set
        {
            isStopped = value;
            OnPropertyChanged("IsTimerStopped");
        }
    }

    public TimeSpan DefaultStopTime { get; set; }

    #endregion

    #region Start Stop Timer

    public void StartTimer()
    {
        dispatcherTimer.Start();
        durationTimeSpan = new TimeSpan(0,0,0);
        startTime = DateTime.Now;
        IsTimerStopped = false;
    }

    public void StopTimer()
    {
        dispatcherTimer.Stop();
        IsTimerStopped = true;
        if (Stopped != null)
        {
            Stopped(this, new EventArgs());
        }
    }

    #endregion


    public void DispatcherTimerTick(object sender, EventArgs e)
    {
       // durationTimeSpan = DateTime.Now - startTime;
       durationTimeSpan = durationTimeSpan.Add(timeInterval);
        DurationTime = string.Format("{0:d2}:{1:d2}:{2:d2}", durationTimeSpan.Hours, durationTimeSpan.Minutes,
                                     durationTimeSpan.Seconds);
        if (DateTime.Now.TimeOfDay >= DefaultStopTime)
        {
            StopTimer();
        }

    }


}

in this class we have the Timer Stopped event

        public event EventHandler Stopped;

in the Method we call the event handler if it is not null

public void StopTimer()
{
    dispatcherTimer.Stop();
    IsTimerStopped = true;
    if (Stopped != null)
    {
       //call event handler
        Stopped(this, new EventArgs());
    }
}

for use this class and timer stop event look at the code

     var timer = new TimerManager();
     timer.Stopped += TimerStopped;
     void TimerStopped(object sender, EventArgs e)
     {
        // do you code
     }
Sign up to request clarification or add additional context in comments.

Comments

-1

If I nright understood what you're asking for, you can do something like this:

public class MyClass
{
  ...
  public  void delegate MuCustomeEventDelegate( ...params...);
  public event MuCustomeEventDelegate MuCustomeEvent;

  ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.