2016-05-02 13 views
0

で特定の時間にコードを実行することができます私は、ボタンのクリックイベントはどのように私はC#の

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 = falsetimer2.Enable = false しかし、私はこれを行う方法がわかりません、あなたは私を助けることができますか?ありがとうございますか?ありがとうございます。

+0

タイマが終了するのを待っているのであれば、なぜそれを使いたいのですか? –

+0

私はあなたが望むように答える方法を教えてくれましたが、クリックハンドラでコードを直接呼び出すのではなく、なぜこれらのタイマーが必要なのでしょうか? – Aconcagua

答えて

0

どこかウェンあなたはタイマーを初期化します。

timer1 += OnTimerElapsed; 
timer2 += OnTimerElapsed; 

と方法:両方のイベントは、自動リセットがfalseに設定されている場合

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 
} 

これは、のみ動作します!そうでなければ、タイマーのいずれかがすでに起動していれば、別のブール値で覚えておく必要があります。

イベントがGUIスレッド以外のスレッドで実行される可能性があるため、BeginInvokeが必要です。