0

I am creating class with event and delegates. Now I want to call the same event handler with multiple class objects at a same time. How can I do it.

My Event Class

public class TimerClass
{

    public delegate void NumberIncrEventHandler(object sender, NumberChangeEventArgs e);
    public event NumberIncrEventHandler NumberIncr;
    private int counter = 0;
    private bool _isStop = false;

    public TimerClass()
    {

    }

    public void start()
    {
        for (int ctr = 0; ctr <= 10; ctr++)
        //while (true)
        {
            if (!_isStop)
            {
                counter++;
                Thread.Sleep(1000);
                NumberChangeEventArgs e = new NumberChangeEventArgs(counter);
                OnNumberReached(e);
            }
            else
            {
                _isStop = false;
                return;
            }
        }
    }

    public void stop()
    {
        _isStop = true;
    }

    protected void OnNumberReached(NumberChangeEventArgs e)
    {
        if (NumberIncr != null)
        {
            NumberIncr(this, e);
        }
    }

}

public class NumberChangeEventArgs : EventArgs
{
    private int _reached;
    public NumberChangeEventArgs(int num)
    {
        this._reached = num;
    }
    public int ReachedNumber
    {
        get
        {
            return _reached;
        }
    }
}

And In my form code is like,

  public partial class frmStartStop : Form
  {

    public ControlClass.TimerClass ctrl1 = new ControlClass.TimerClass();
    public ControlClass.TimerClass ctrl2 = new ControlClass.TimerClass();

    public frmStartStop()
    {
        InitializeComponent();
        ctrl1.NumberIncr += new ControlClass.TimerClass.NumberIncrEventHandler(this.ctrl1_Tick);
        ctrl2.NumberIncr += new ControlClass.TimerClass.NumberIncrEventHandler(this.ctrl2_Tick);
    }

    private void btnStart2_Click(object sender, EventArgs e)
    {
        ctrl2.start();
    }

    private void btnStop2_Click(object sender, EventArgs e)
    {
        ctrl2.stop();
    }

    private void ctrl2_Tick(object sender, NumberChangeEventArgs e)
    {
        txtCount2.Text = e.ReachedNumber.ToString();
        Application.DoEvents();
    }

    private void btnStart1_Click(object sender, EventArgs e)
    {
        ctrl1.start();
    }

    private void btnStop1_Click(object sender, EventArgs e)
    {
        ctrl1.stop();
    }

    private void ctrl1_Tick(object sender, NumberChangeEventArgs e)
    {
        txtCount1.Text = e.ReachedNumber.ToString();
        Application.DoEvents();
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }


}

Now when I run my application both tick events are not working individual. So, What I need to do to run individual event.

Andrei
  • 55,890
  • 9
  • 87
  • 108
  • You are running your loop in the `start()` method, in the single UI thread. This is why you found yourself calling `Application.DoEvents()`, a method you should _never_ ever call. See marked duplicate for advice on how to execute your code without blocking the UI thread. You will find that you also run into issues setting the `Text` property in your handler; see `Control.Invoke()` for that. See also `System.Windows.Forms.Timer`. It's fine if you want to experiment to learn programming, but there's no need to reinvent the wheel for real-world code. – Peter Duniho Jun 30 '17 at 17:46
  • Thanks Peter for replying. Actually as per customer requirement I will not use timer control. I tried to apply control.Invoke() but it is not working. – Krunal Vakhariya Jul 03 '17 at 06:13
  • `Control.Invoke()` is only relevant once you have the other aspects working. It's not going to solve your "blocked UI thread" problem. As for the "customer requirement", I guess you're stuck with them, but you should try to find customers that won't micro-manage your code design, especially in ways that force you into writing the code _wrong_. – Peter Duniho Jul 03 '17 at 06:16
  • Hi Peter, Is it possible to add thread and execute both event independently on above code? If yes then How can I update my code accordingly? – Krunal Vakhariya Jul 04 '17 at 13:11
  • _"Is it possible to add thread and execute both event independently"_ -- yes, of course. You can move the code now in the `start()` method, to a separate method that `start()` calls, as in `start() { Task.Run(() => runTimer()); }`. It would also make sense to use `Progress` instead of the event you've declared; alternatively, you can use `Progress` as an implementation detail for your event (i.e. create the `Progress` object inside the `TimerClass` class and just use it in there to raise the event). – Peter Duniho Jul 04 '17 at 18:46
  • Hi Peter, I applied thread and now its working fine.Thanks for your support. :) – Krunal Vakhariya Jul 06 '17 at 04:31

0 Answers0