我有一个按钮点击事件代码
private void bt_exchange_Click(object sender, EventArgs e)
{
timer1.Start();
timer2.Start();
MessageBox.Show("Alice received Tb, Bob received Ta", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("They now have common Session Key", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
key = ((Math.Min(Ta, Tb) == Tb) ? XpowYmodN(Tb, Sa, _p) : XpowYmodN(Ta, Sb, _p));
tb_key_a.Text = tb_key_b.Text = key.ToString();
Enable("key");
}
我想要这些代码
MessageBox.Show("Alice received Tb, Bob received Ta", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("They now have common Session Key", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
key = ((Math.Min(Ta, Tb) == Tb) ? XpowYmodN(Tb, Sa, _p) : XpowYmodN(Ta, Sb, _p));
tb_key_a.Text = tb_key_b.Text = key.ToString();
Enable("key");
将在timer1_Tick()
和timer2_Tick()
个事件结束后执行(表示timer1.Enabled = false
和timer2.Enable = false
但我不知道该怎么做,你能帮助我吗?非常感谢你。
答案 0 :(得分:0)
你可以在某处初始化定时器:
timer1 += OnTimerElapsed;
timer2 += OnTimerElapsed;
方法:
delegate void CreateKeyDelegate();
static void OnTimerElapsed(Object source, System.Timers.ElapsedEventArgs e)
{
if(!(timer1.Enabled || timer2.Enabled))
{
Control control; // any of your GUI controls!
control.BeginInvoke(new CreateKeyDelegate(CreateKey));
}
}
void CreateKey()
{
// your code here
}
这只有在两个事件都将AutoReset设置为false时才有效!如果任何定时器已经解雇,你必须记住单独的布尔值。
BeginInvoke是必要的,因为事件可能在除GUI线程之外的另一个线程上运行。