2011-09-11 24 views
3

我一直在研究一個程序,它有3個類,其中2個類有定時器以不同的時間間隔重複,一旦定時器的一個「週期」完成,它將引發一個字符串作爲返回的事件。第三類訂閱來自其他兩個計時器類的事件並將它們打印到屏幕上。它工作得很好!我如何訂閱募集活動並一起打印?

但我的問題是,它是單獨打印它們。比方說,第一個計時器類運行,然後每隔2分鐘提出一個「hello」,每秒一次提出另一個「dog」,每發生一次事件,它都會將提出的事件打印到控制檯。我希望它每秒鐘打印「hellodog」,並將第一個計時器(hello)的值存儲在專用字段中,所以即使計時器(較慢的2分鐘計時器)還沒有被解僱。當2分鐘計時器啓動時,它將更新值,無論新值是什麼,新值將被打印到屏幕直到它再次觸發。

如果它很混亂,我會很樂意澄清。其種類很難解釋。

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()); 
     } 
    } 
} 
+2

牆,削減下來。 –

+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. 
    } 
1

糾正我,如果我誤解了問題,但它聽起來像是要協調兩個計時器事件(打印「hellodog」)的迴應。

在我看來,最簡單的方法是隻使用一個計時器,並讓計時器的事件處理程序計算處理程序被調用的次數,以決定是否採用每秒一次行動,或者採取每兩分鐘一次的行動。由於慢速計時器是快速計時器的精確倍數,因此您只需設置一個觸發每秒的計時器,並且每1秒計時器的120次調用(120秒= 2分鐘)也執行2分鐘的動作)。

+0

是否有可能製造另一個計時器每秒關閉?所以每個引發的事件都會在「輸出」類中更新兩個新字符串,然後新計時器會每秒讀取這些字符串?這可能嗎? – Csharpz

+0

@Csharpz:我想你會遇到定時器引發的問題。我不知道有一個保證,如果計時器應該「同時」啓動,那麼計時器將首先啓動。有可能他們可以在幾秒鐘之前或之後發射幾毫秒(可能有關於訂單和時間的強有力的保證,但我並不知道)。 –

1

我想我明白你想要什麼,那就是同步兩個計時器的輸出。恐怕除了闖過它之外,沒有辦法做到這一點。設置一大堆布爾變量,用於跟蹤每個事件是否觸發以及是否將同步消息發送到輸出。

1

這應該做你想做的。

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

     var value1 = ""; 
     var value2 = ""; 

     Action writeValues =() => Console.WriteLine(value1 + value2); 

     timer1.NewStringAvailable += (s, e) => 
     { 
      value1 = e.Value; 
      writeValues(); 
     }; 

     timer2.NewStringAvailable += (s, e) => 
     { 
      value2 = e.Value; 
      writeValues(); 
     }; 

     Console.ReadLine(); 
    } 

讓我知道這是否正確。乾杯。

1

第二個(更快)計時器應該是唯一一個打印。 第一個(較慢)定時器只應更新第二個定時器將使用的字符串。

在 '輸出' 類(你可以把它放在主前):

string string1; 

,然後:

的代碼
static void timer1_NewStringAvailable(object sender, BaseClassThatCanRaiseEvent.StringEventArgs e) 
{ 
    string1 = e.Value; 
} 

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

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