2011-09-11 70 views
1

我一直在編寫程序。我有3個班。其中2個類具有以不同間隔重複的定時器,並且一旦完成定時器的一個「循環」,它就引發一個字符串作爲返回的事件。第三類從另外兩個定時器類訂閱事件並將它們打印到屏幕上。它工作得很好!在C#中引發事件並輸出?

但我的問題是,它分別打印它們。說當前第一個定時器類運行,然後每隔2分鐘提出一個「你好」,每秒另一個類「狗」。那麼每次發生事件時都會將引發的事件打印到控制檯。我希望它每秒鐘都會打印出「hellodog」。

我在想:所以每當計時器觸發時,它將引發一個事件並用當前值更新「輸出」類中的字符串,然後使另一個計時器每秒關閉,此計時器將讀取更新的字符串一起作爲一個輸出,如「hellodog」。這是可能的,如果這是我認爲最簡單的方式。我將如何實現這個想法?

如果它令人困惑,我會澄清。

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-raised-events-and-printing-together) –

+0

另請參閱http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed -from-posts –

+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(sender == FormWithTimer)我得到的錯誤是用作變量的類型。但也許我可以修復它。有小費嗎? – Csharpz

+1

對不起,應該是發件人==計時器1和發件人==計時器2 – CharithJ

+0

ahh謝謝,但現在我得到一個對象引用是必需的非靜態方法或字段Output.timer1 – Csharpz

1

在您的示例中處理事件時,他們無法訪問有關其他事件的信息。如果您希望有2個事件更新字符串,但您希望處理程序打印來自兩個更新的字符串的數據,則需要事件處理程序有權訪問這兩個字符串。您可以將它們存儲在事件處理類的變量中,或者將它們作爲引發事件的類的公共屬性。這樣在任何一個事件處理程序中,您都可以訪問來自其他事件的更新字符串。