Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

C# - Eigene Events - Beispiel anhand einer E-Mail-Überwachung

benjamin-abt.com
March 12, 2013 3 min read BEN ABT
Csharp

Dieser Artikel stammt aus dem Jahr 2013. Versionen, Werkzeuge und Empfehlungen können sich seitdem geändert haben. Neuere Beiträge findest du unter Csharp.

Dieser Codeschnippsel soll exemplarisch zeigen, wie man in C# eigene Events definiert.

Als Beispiel dient hier die Überwachung von E-Mails in der Konsole.

C#
  1    /// <summary>
  2    /// Ein Objekt dieser Klasse wird an den Ereignis-Abonnenten geliefert
  3    /// </summary>
  4    public class NeueEMailArgs : EventArgs
  5    {
  6        // Eigenschaften, die uns in diesem Beispiel interessiere.
  7        // Können nach Bedarf erweitert werden.
  8        public String Betreff { get; private set; }
  9        public String Absender { get; private set; }
 10
 11        // Konstruktor mit den Standard-Parametern, die wir als Pflicht ansehen
 12        public NeueEMailArgs( String absender, String betreff )
 13        {
 14            // Zuweisen
 15            this.Betreff = betreff;
 16            this.Absender = absender;
 17        }
 18    }
 19
 20    /// <summary>
 21    /// Klasse, die ein Postfach überwacht und über neue EMail benachrichtigt
 22    /// </summary>
 23    public class EMailUeberwachung
 24    {
 25        /// <summary>
 26        /// Das Postfach, das überwacht werden soll
 27        /// </summary>
 28        private readonly MailPostfach _mailPostfach;
 29
 30        /// <summary>
 31        /// Konstrktor, der das Postfach empfängt, das er überwachen soll
 32        /// </summary>
 33        /// <param name="mailPostfach"></param>
 34        public EMailUeberwachung( MailPostfach mailPostfach )
 35        {
 36
 37            _mailPostfach = mailPostfach;
 38        }
 39
 40        public delegate void MyNeueEMailHandler( object sender, NeueEMailArgs e );
 41
 42        // Definieren eines Event Handlers
 43        public event MyNeueEMailHandler OnNeueEMail;
 44
 45        /// <summary>
 46        /// Diese Methode feuert letzten Endes das Ereignis ab.
 47        /// Der Abonnent erhält die Informationen, die wir ihm über die Ereignisargumente geben.
 48        /// </summary>
 49        /// <param name="e"></param>
 50        private void FeuerNeueEMail( NeueEMailArgs e )
 51        {
 52            // Prüfen, ob überhaupt irgendjemand den Event abonniert hat
 53            if ( OnNeueEMail != null )
 54            {
 55                // Ja, der Event hat mindestens einen Abonnenten
 56                OnNeueEMail( this, e );
 57            }
 58        }
 59
 60        public void Start( )
 61        {
 62            // Hier befindet sich nun die Logik, die das Postfach überwachen soll
 63            // ....
 64
 65            // Es wurde eine neue Mail gefunden
 66            var emailArgs = new NeueEMailArgs( "ab@sen.der", "Es wurde eine neue Mail empfangen" );
 67
 68            // Event abfeuern
 69            FeuerNeueEMail( emailArgs );
 70        }
 71    }
 72    /// <summary>
 73    /// Inhalt nicht definiert; dient hier nur zur Komplettierung des Beispiels
 74    /// </summary>
 75    public class MailPostfach
 76    {
 77
 78    }
 79
 80    /// <summary>
 81    /// Die eigentliche Anwendung
 82    /// </summary>
 83    public class Programm
 84    {
 85        public static void Main( params String[ ] args )
 86        {
 87            // Definieren unseres Postfachs
 88            var myPostfach = new MailPostfach( );
 89
 90            // Damit die Ovberfläche bedienbar bleibt, während im Hintergrund nach neuen Mails geschaut wird, lagern wir die Überwachung in einen eigenen Task aus.
 91            var eMailueberwachungTask = Task.Factory.StartNew( ( ) =>
 92                {
 93                    // Starten der E-Mail-Überwachung
 94                    EMailUeberwachung mailUeberwachung = new EMailUeberwachung( myPostfach );
 95
 96                    mailUeberwachung.OnNeueEMail += OnNeueEMail;
 97                } );
 98
 99            // Auf Beenden des Benutzers warten.
100            Console.WriteLine( "Die E-Mail überwachung ist aktiv. Drücken Sie eine Taste nach Wahl um die Anwendung zu beenden." );
101            Console.ReadKey( );
102
103            // Ein sauberes Beenden des Tasks der E-Mail-Überwachung würde über den CancellationTokenSource laufen;
104            // dies soll aber nicht Bestandteil dieses Beispielssein
105
106            // Anwendung wird beendet
107
108        }
109
110        /// <summary>
111        /// Diese Methode wird aufgerufen, wenn die Überwachung eine neue E-Mail erhält.
112        /// Achtung: Invoke bei Zugriff auf den GUI-Thread nötig, da Hintergrund-Task.
113        /// </summary>
114        static void OnNeueEMail( object sender, NeueEMailArgs e )
115        {
116            Console.WriteLine( "[!!!] Neue EMail: Absender: " + e.Absender + "- Betreff: " + e.Betreff );
117        }
118    }

Related articles

Production Rate Limiting in ASP.NET Core

Nov 3, 2026 - 16 min read

Production Rate Limiting in ASP.NET Core

Most ASP.NET Core rate-limiting examples stop after registering a fixed window. That is enough to demonstrate the middleware, but it is not …

The Big Decimal Bugfix with .NET 11

Nov 2, 2026 - 11 min read

The Big Decimal Bugfix with .NET 11

For more than two decades, (decimal)1.23 returned 1.23m. It looked correct, it passed every test and it was a polite lie.

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026 - 10 min read

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled and Alpine are often compared as three equivalent Linux distributions. They are not. Alpine is a distribution built …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (41) DevOps (17) Docker (15) Microsoft (14) EF Core (11) Microsoft MVP (11) Security (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Synology (9) .NET 11 (8) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact