简体   繁体   English

如何在C#中同时触发两个事件

[英]How to fire two events at the same time in c#

I have three buttons on my windows form 我在Windows窗体上有三个按钮

在此处输入图片说明

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 . 并且每个按钮都有它自己的方法,除了To + T1时 ,当我按下To时 ,它将把时间T0设置为“ 0”以及一些其他设置,类似地,当我按下T1时,它将获得当前时间并计算时间T1

Button T0+T1 will fire T0 button click event followed by T1 button click event. 按钮T0 + T1将触发T0按钮单击事件,然后触发T1按钮单击事件。


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 ? 从以上设置中,无论哪种方式,我都可以获得相同的T0T1计时(例如:T0 = 0,T1 = 0.1sec),现在我的问题是,如果我按下T0 + T1按钮,有没有办法实现与T0一样的计时= 0T1 = 0

any suggestions would be much appreciated & Thanks for your time..:) 任何建议将不胜感激,谢谢您的时间.. :)

In the Form.Designer.cs you can add another System.Eventhandler. 在Form.Designer.cs中,您可以添加另一个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. 但是,以上代码将在彼此之后触发事件,但不会同时触发。

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. 不是 Button0.ClickButton1.Click事件触发。 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. 第二,你不能叫正好因为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). 当然,使用单线程是不可能的(就像您的示例一样),因为线程按顺序执行其指令,但是使用多线程是不可能的-不能保证两个线程将同时启动(并且没有这样的API)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM