Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/integrativesoft/asyncevents
Async Events for C#
https://github.com/integrativesoft/asyncevents
async async-events async-programming asynctask csharp events net-standard
Last synced: 12 days ago
JSON representation
Async Events for C#
- Host: GitHub
- URL: https://github.com/integrativesoft/asyncevents
- Owner: integrativesoft
- License: mit
- Created: 2019-12-14T02:39:25.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-29T01:31:05.000Z (over 4 years ago)
- Last Synced: 2024-05-20T17:45:50.128Z (8 months ago)
- Topics: async, async-events, async-programming, asynctask, csharp, events, net-standard
- Language: C#
- Size: 12.7 KB
- Stars: 6
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AsyncEvents for C#
This library adds support for asynchronous events in C#
```csharp
// BEFORE: Declaration for a regular synchronous event:
public event MyEvent OnChange;// AFTER: Declaration for an asynchronous event:
public AsyncEvent OnChangeAsync { get; } = new AsyncEvent();
```# Sample application
```csharp
using Integrative.Async;
using System;
using System.Threading.Tasks;namespace AsyncEventsSample
{
class Program
{
// instance that raises async events
readonly static MyEventSource source = new MyEventSource();// program's Main
static async Task Main()
{
Console.WriteLine("Hello World!");
source.OnChangeAsync.Subscribe(ShowCounterAsync);
await source.IncreaseAsync();
Console.WriteLine("Done!");
}// handler for events
static Task ShowCounterAsync(object sender, CounterChangedEventArgs args)
{
Console.WriteLine($"Counter value: {args.Counter}");
return Task.CompletedTask;
}
}// class for async event parameters
class CounterChangedEventArgs : EventArgs
{
public int Counter { get; set; }
}// class that raises events
class MyEventSource
{
int counter;// async event declaration
public AsyncEvent OnChangeAsync { get; }
= new AsyncEvent();// method that triggers events
public async Task IncreaseAsync()
{
counter++;
await OnChangeAsync.InvokeAsync(this, new CounterChangedEventArgs
{
Counter = counter
});
}
}
}```