How to: Raise and Consume Events

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Counter counter = new Counter(new Random().Next(10));
            // 4. subscribe event
            counter.ThresholdReached += counter_ThresholdReached;

            Console.WriteLine("press ‘a‘ key to increase total");
            while (Console.ReadKey(true).KeyChar == ‘a‘)
            {
                Console.WriteLine("adding one");
                counter.Add(1);
            }
        }

        static void counter_ThresholdReached(object sender, ThresholdEventArgs e)
        {
            Console.WriteLine("Threshold " + e.ThresholdNum + "  reached at time "+ e.TimeReached);
        }
    }

    public class Counter
    {
        int threshold;
        int total;
        public Counter(int thresholdVal)
        {
            threshold = thresholdVal;
        }

        public void Add(int x)
        {
            total += x;

            // 5. trigger event
            if (total > threshold)
            {
                ThresholdEventArgs args = new ThresholdEventArgs();
                args.ThresholdNum = threshold;
                args.TimeReached = DateTime.Now;

                OnThresholdReached(this, args);
            }
        }

        // 1. define event
        public event EventHandler<ThresholdEventArgs> ThresholdReached;

        // 2. define OnXXX virtual method
        public virtual void OnThresholdReached(object sender, ThresholdEventArgs e)
        {
            EventHandler<ThresholdEventArgs> handler = ThresholdReached;
            if (handler != null)
                handler(this, e);
        }
    }

    // 3. define event arguments
    public class ThresholdEventArgs : EventArgs
    {
        public int ThresholdNum { get; set; }
        public DateTime TimeReached { get; set; }
    }
}

How to: Raise and Consume Events,布布扣,bubuko.com

时间: 2024-11-03 22:37:13

How to: Raise and Consume Events的相关文章

Scheduling Jobs with Oracle Scheduler

In this chapter: About Scheduler Objects and Their Naming Creating, Running, and Managing Jobs Creating and Managing Programs to Define Jobs Creating and Managing Schedules to Define Jobs Using Events to Start Jobs Creating and Managing Job Chains Pr

C#基础-事件 继承类无法直接引发基类的事件

An event can be raised only from the declaration space in which it is declared. Therefore, a class cannot raise events from any other class, even one from which it is derived. 事件只能在它被声明的声明空间(类)中使用.所以不能从任何其他类引发,即使该类是事件所在类的继承类. 那我们如何才可以引发基类中的事件,给个实例大家一

RabbitMQ的安装和使用Python连接RabbitMQ

绪论 这里的环境使用的是Mac OS X系统,所有的配置和使用都是基于Mac OS X 和Python 2.7 以及对应的pika库的. RabbitMQ的安装和配置 安装部分 #brew install rabbitmq 配置和启动 #sudo brew services start rabbitmq #sudo rabbitmqctl add_user admin admin "创建用户(username password)" #sudo rabbitmqctl set_user_

Benchmarking Zeebe: An Intro to How Zeebe Scales Horizontally and How We Measure It

Written by Felix Müller and Mike Winters on Jun 12 2018 in the Inside Zeebe category. In the past few weeks, we’ve mentioned Zeebe’s performance in horizontal scalability benchmarks that we run internally, but we haven’t yet explained how exactly we

云原生生态周报 Vol. 13 | Forrester 发布企业级容器平台报告

业界要闻 近日,全球知名市场调研机构 Forrester 发布首个企业级公共云容器平台报告.其中,阿里云容器服务的市场表现全球前三.中国第一,同时创造中国企业最好成绩,进入强劲表现者象限.报告显示,阿里云容器服务市场表现为中国第一,与谷歌云并列全球第三. Forrester 分析师认为:“阿里云容器服务提供了广泛的开发和应用服务支持能力,并且具备丰富的市场生态和合作伙伴体系,是企业在中国寻求完备容器云服务能力的最佳选择. Virtual Kubelet 开源项目发布第一个可商用 1.0 版本,本

cakephp 的事件系统(Getting to grips with CakePHP’s events system), 基于观察者模式

This article was written about CakePHP 2.x and has been untested with CakePHP 3.x CakePHP seems to get a slightly unfavourable reputation when compared to the likes of Symfony orZend Framework due to its lack of namespaces and not playing nicely with

wxPython StyledTextCtrl events

wxStyledTextCtrl - Events   Copyright and License information  Home __  A  B  C  D  E  F  G  H  I  L  M  P  R  S  T  U  V  W wxStyledTextCtrl - Events Event Masking GetModEventMask SetModEventMask Events EVT_STC_CHANGE EVT_STC_CHARADDED EVT_STC_DO_DR

C#中的弱事件(Weak Events in C#)

(原创翻译文章·转载请注明来源:http://blog.csdn.net/hulihui) 原文:Weak Events In C#: Different approaches to weak events. by Daniel Grunwald.  Download source code - 15.5 KB 翻译前序 翻译后记 目录 引言 究竟什么是事件? 第1部分:监听方(Listener-side)的弱事件 解决方案0:仅仅注销 解决方案1:事件调用后注销 解决方案2:带弱引用(Weak

【转】Python线程同步机制: Locks, RLocks, Semaphores, Conditions, Events和Queues

Python线程同步机制: Locks, RLocks, Semaphores, Conditions, Events和Queues | Comments 翻译自Laurent Luce的博客原文名称:Python threads synchronization: Locks, RLocks, Semaphores, Conditions, Events and Queues原文连接:http://www.laurentluce.com/posts/python-threads-synchron