0

I have three buttons on my windows form

enter image description here

and each button has got it's own method except To+T1, when I hit on To, it'll setup the time T0 to be '0' along with some additional setup, similarly when I hit on T1 it gets the current time and calculates time T1.

Button T0+T1 will fire T0 button click event followed by T1 button click event.


from the above setup, either way I get the same timings for T0 & T1(ex:T0=0, T1=0.1sec), now My question is, If I hit T0+T1 button is there any way to achieve timings as T0=0, T1=0?

any suggestions would be much appreciated & Thanks for your time..:)

SanVEE
  • 2,009
  • 5
  • 34
  • 55

2 Answers2

5

In the Form.Designer.cs you can add another System.Eventhandler.

For example you can change the following:

this.ToT1.Click += new System.EventHandler(this.ToT1_Click);

To this:

this.ToT1.Click += new System.EventHandler(this.To_Click);
this.ToT1.Click += new System.EventHandler(this.T1_Click);

However the above code will trigger the events after eachother, but not at the same time.

Robin
  • 1,927
  • 3
  • 18
  • 27
  • 2
    I'd add this code in the `Form.cs` file's constructor, rather than in the `Form.Designer.cs` file's `InitializeComponent()` method. The reason being: As soon as you use the designer to change your form, your code will be automatically deleted. Needless to say, you'll have to call `InitializeComponent()` before setting the `EventHandler`s – Nolonar Jun 28 '13 at 10:43
  • Thanks for the solution, this is good for simple solutions, since I require the timings to precise and noticed that I still see some delay beween T0_Click & T1_Click methods – SanVEE Jun 28 '13 at 11:08
  • @Nolonar I wrote this in a hurry. Apologies. – Robin Jun 28 '13 at 11:27
4

Looks like you just need to provide some base value for calculation:

void CalculateT0(DateTime calculationBase) {...}
void CalculateT1(DateTime calculationBase) {...}

void T0_ButtonClick(...)
{
    CalculateT0(DateTime.Now);
}

void T1_ButtonClick(...)
{
    CalculateT1(DateTime.Now);
}

void T0Plus1_ButtonClick(...)
{
    var calculationBase = DateTime.Now;
    CalculateT0(calculationBase);
    CalculateT1(calculationBase);
}

Update.

About firing two events at the same time.

The first, you're question has nothing about firing events. You have such kind of code now:

void T0Plus1_ButtonClick(...)
{
    T0_ButtonClick(...);
    T1_ButtonClick(...);
}

This is not an Button0.Click or Button1.Click event firing. This code just call two methods (even if they're used as event handlers somewhere). You can't fire an event outside the object, where that event declared.

The second, you can't call any two methods at exactly the same time because of OS nature, it is just not a real-time OS. Of course, this is impossible using single thread (like in your example), because thread executes its instructions sequentially, but using multiple threads this is impossible to - there's no guarantee, that two threads will start simultaneously (and there's no such API).

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • Thanks for the quick solution, this one seems to wok but would me more interested to know if there's a way to fire two events at the same time(just for learning) – SanVEE Jun 28 '13 at 10:52
  • 1
    Event firing is not much different from calling methods. Most of all, it is synchronous. So you cannot fire two events at the same time in the same way that you cannot call two methods at the same time. Of course, you could have two threads for that, but that would really be like driving a nail with a wrecking ball. – Jan Dörrenhaus Jun 28 '13 at 10:59
  • 1
    I've updated the answer to clarify "the same time" confusion. – Dennis Jun 28 '13 at 11:13