如何订阅凸起的事件和打印在一起? [英] How do I subscribe to raised events and printing together?

查看:108
本文介绍了如何订阅凸起的事件和打印在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个程序,它有3个类,其中2个类具有以不同间隔重复的定时器,并且一旦定时器的一个周期完成,它会引发一个字符串作为返回的事件。第三类订阅来自其他两个定时器类的事件并将其打印到屏幕。它的效果很好!



但是我的问题是它是单独打印的。让我们说,第一个计时器类运行,然后每2分钟引发你好,另一个类狗每秒,每次提出一个事件时,将引发的事件打印到控制台。我想要它每隔一秒打印一次hellodog,并将第一个定时器(hello)的值存储在一个私人字段或某些东西,所以它仍然打印到屏幕,即使定时器(较慢的2分钟定时器)还没有被解雇。当2分钟的定时器触发时,它会将值更新为任何新的值,并将新值打印到屏幕,直到再次触发。



如果令人困惑我将乐意澄清。它的种类很难解释。

 命名空间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;

//来自定时器1的theString的东西
Console.WriteLine(theString);
}

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

//来自定时器2的'theString2'的东西
Console.WriteLine(theString2);
}
}

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

public string Value {get;私人集合}
}

//人们可以订阅
的事件本身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();

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

timer.Elapsed + = new ElapsedEventHandler(timer_Tick); //每次定时器ticks,timer_Tick将被称为
timer.Interval =(200000); //计时器会勾选10秒
timer.Enabled = true; //启用定时器
timer.Start(); //启动计时器
}

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


公共部分类FormWithTimer2:BaseClassThatCanRaiseEvent
{
定时器定时器=新的Timer();

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

timer.Elapsed + = new ElapsedEventHandler(timer_Tick2); //每次定时器ticks,timer_Tick将被称为
timer.Interval =(1000); //计时器会勾选10秒
timer.Enabled = true; //启用定时器
timer.Start(); //启动计时器
}

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


解决方案

您可以为这两个计时器使用相同的事件处理程序。并通过标识发件人构建输出。 (没有测试语法错误的代码。)

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

如果(timer1Value!= String.Empty&&&&&&&#; Timer2Value!= String.Empty)
{
Console.WriteLine(timer1Value + timer2Value);
//根据需要进行字符串连接。
}


I have been working on a program that has 3 classes of which 2 of the classes have timers that repeat at different intervals and once one "cycle" of the timer is done it raises an event with a string as return. The 3rd class subscribes to the events from the other two timer classes and prints them to screen. it works great!

But my issue is that it is printing them separately. Lets say that the first timer class runs and then raises "hello" every 2 minutes and the other class "dog" every second and every time an event is raised it prints the raised event to console. I would want it to instead print "hellodog" every second and store the value of the first timer(hello) in a private field or something so it still prints to screen even if the timer(the slower 2 minute timer) hasn't been fired. and when the 2 minute timer fires it updates the value to whatever the new one is and that new value get printed to screen until it fires again.

If it is confusing I will gladly clarify. its kind of hard to explain.

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

解决方案

You can use the same event handler for both timers. And construct the output by identifying the senders. (Didn't test the code for syntax errors.)

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

这篇关于如何订阅凸起的事件和打印在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆