2011-09-11 23 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); 
}