2011-09-11 4 views
1

iveはプログラムで作業しています。私は3つのクラスを持っています。 2つのクラスには異なる間隔で繰り返すタイマーがあり、タイマーの1つの「サイクル」が完了すると、それはリターンとして文字列を含むイベントを発生させます。第3クラスは、他の2つのタイマークラスからのイベントにサブスクライブし、それらをスクリーンにプリントします。それは素晴らしい作品です!C#でイベントを発生させて出力しますか?

しかし、私の問題は、それらを別々に印刷することです。現在、最初のタイマークラスが実行され、2分ごとに "hello"が呼び出され、毎秒他のクラス "dog"が呼び出されます。イベントが発生するたびに、発生したイベントがコンソールに出力されます。私は代わりに毎秒 "hellodog"を印刷したいと思うでしょう。

私は考えていました:タイマーが起動するたびにイベントが発生し、現在の値で「出力」クラスの文字列を更新し、毎秒オフになる別のタイマーを作成します。更新された文字列を "hellodog"のように1つの出力としてまとめました。これが可能ならばこれが私が考える最も簡単な方法です。どのように私はこのアイデアを達成するだろうか?

紛らわしい場合は明示します。

namespace Final 
{ 
    public class Output 
    { 
     public static void Main() 
     { 
      var timer1 = new FormWithTimer(); 
      var timer2 = new FormWithTimer2(); 

      timer1.NewStringAvailable += new EventHandler<BaseClassThatCanRaiseEvent.StringEventArgs>(timer1_NewStringAvailable); 

      timer2.NewStringAvailable += new EventHandler<BaseClassThatCanRaiseEvent.StringEventArgs>(timer2_NewStringAvailable); 
      Console.ReadLine(); 
     } 

     static void timer1_NewStringAvailable(object sender, BaseClassThatCanRaiseEvent.StringEventArgs e) 
     { 
      var theString = e.Value; 

      //To something with 'theString' that came from timer 1 
      Console.WriteLine(theString); 
     } 

     static void timer2_NewStringAvailable(object sender, BaseClassThatCanRaiseEvent.StringEventArgs e) 
     { 
      var theString2 = e.Value; 

      //To something with 'theString2' that came from timer 2 
      Console.WriteLine(theString2); 
     } 
    } 

    public abstract class BaseClassThatCanRaiseEvent 
    { 
     public class StringEventArgs : EventArgs 
     { 
      public StringEventArgs(string value) 
      { 
       Value = value; 
      } 

      public string Value { get; private set; } 
     } 

     //The event itself that people can subscribe to 
     public event EventHandler<StringEventArgs> NewStringAvailable; 

     protected void RaiseEvent(string value) 
     { 
      var e = NewStringAvailable; 
      if (e != null) 
       e(this, new StringEventArgs(value)); 
     } 
    } 

    public partial class FormWithTimer : BaseClassThatCanRaiseEvent 
    { 
     Timer timer = new Timer(); 

     public FormWithTimer() 
     { 
      timer = new System.Timers.Timer(200000); 

      timer.Elapsed += new ElapsedEventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called 
      timer.Interval = (200000);    // Timer will tick evert 10 seconds 
      timer.Enabled = true;      // Enable the timer 
      timer.Start();        // Start the timer 
     } 

     void timer_Tick(object sender, EventArgs e) 
     { 
      ... 
      RaiseEvent(gml.ToString());      
     } 
    } 


    public partial class FormWithTimer2 : BaseClassThatCanRaiseEvent 
    { 
     Timer timer = new Timer(); 

     public FormWithTimer2() 
     { 
      timer = new System.Timers.Timer(1000); 

      timer.Elapsed += new ElapsedEventHandler(timer_Tick2); // Everytime timer ticks, timer_Tick will be called 
      timer.Interval = (1000);    // Timer will tick evert 10 seconds 
      timer.Enabled = true;      // Enable the timer 
      timer.Start();        // Start the timer 
     } 

     void timer_Tick2(object sender, EventArgs e) 
     { 
      ... 
      RaiseEvent(aida.ToString()); 
     } 
    } 
} 
+3

可能重複http://stackoverflow.com/questions/7375452/how-do -i-subscribe-to-raised-events-and-printing-together) –

+0

また、http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removedを参照してください。投稿から –

+0

私は理解している – Csharpz

答えて

1

両方のタイマに同じイベントハンドラを使用できます。また、送信者を特定して出力を構成します。 (構文エラーのコードをテストしていませんでした。)

private static string timer1Value = string.Empty; 
private static string timer2Value = string.Empty; 
private static FormWithTimer timer1; 
private static FormWithTimer2 timer2; 

public static void Main() 
{ 
    timer1 = new FormWithTimer(); 
    timer2 = new FormWithTimer2(); 

    timer1.NewStringAvailable += new EventHandler<BaseClassThatCanRaiseEvent.StringEventArgs>(timer1_NewStringAvailable); 

    timer2.NewStringAvailable += new EventHandler<BaseClassThatCanRaiseEvent.StringEventArgs>(timer1_NewStringAvailable); 
    Console.ReadLine(); 
} 


static void timer1_NewStringAvailable(object sender, BaseClassThatCanRaiseEvent.StringEventArgs e) 
{ 
    if (sender == timer1) 
    { 
     timer1Value = e.Value.ToString(); 
    } 
    else if (sender == timer2) 
    { 
     timer2Value = e.Value.ToString(); 
    } 

    if (timer1Value != String.Empty && timer2Value != String.Empty) 
    { 
     Console.WriteLine(timer1Value + timer2Value); 
     // Do the string concatenation as you want. 
    } 
[私は一緒に発生したイベントや印刷に加入するにはどうすればよい?](の
+0

if(送信者== FormWithTimer)私はエラーが発生するタイプは変数として使用されます。多分私はそれを修正することができます。任意のヒント? – Csharpz

+1

残念ですが送信者== timer1と送信者== timer2 – CharithJ

+0

ああありがとうございます。しかし、今はオブジェクト参照が必要です。非staticメソッドまたはフィールドOutput.timer1 – Csharpz

1

例でイベントが処理されると、他のイベントに関する情報にアクセスすることはできません。文字列を更新するイベントが2つありたいが、ハンドラでのデータを両方とも更新する場合は、の文字列を更新する必要があります。イベントハンドラは両方の文字列にアクセスする必要があります。イベント処理クラスの変数に格納するか、イベントを発生させているクラスのパブリックプロパティにすることができます。そうすれば、どちらのイベントハンドラでも、他のイベントから更新された文字列にアクセスすることができます。

関連する問題