Question

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..:)

Was it helpful?

Solution 2

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).

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top