Skip to content

Commit 0ebb161

Browse files
committed
problem: missing docs for async await
1 parent a8abd20 commit 0ebb161

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

docs/async-await.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Async Await
2+
===========
3+
4+
Since version (4.0.0.239-pre) NetMQ support async/await.
5+
6+
To use async/await feature you need to create a `NetMQRuntime`.
7+
8+
## Example:
9+
10+
```csharp
11+
async Task ServerAsync()
12+
{
13+
using (var server = new RouterSocket("inproc://async"))
14+
{
15+
for (int i = 0; i < 1000; i++)
16+
{
17+
var (routingKey, more) = await server.ReceiveRoutingKeyAsync();
18+
var (message, _) = await server.ReceiveFrameStringAsync();
19+
20+
// TODO: process message
21+
22+
await Task.Delay(100);
23+
server.SendMoreFrame(routingKey);
24+
server.SendFrame("Welcome");
25+
}
26+
}
27+
}
28+
29+
async Task ClientAsync()
30+
{
31+
using (var client = new DealerSocket("inproc://async"))
32+
{
33+
for (int i = 0; i < 1000; i++)
34+
{
35+
client.SendFrame("Hello");
36+
var (message, more) = await client.ReceiveFrameStringAsync();
37+
38+
// TODO: process reply
39+
40+
await Task.Delay(100);
41+
}
42+
}
43+
}
44+
45+
static void Main(string[] args)
46+
{
47+
using (var runtime = new NetMQRuntime())
48+
{
49+
runtime.Run(ServerAsync(), ClientAsync());
50+
}
51+
}
52+
```
53+
54+
NetMQRuntime is a wrapper over NetMQPoller, when calling an async function the socket is automatically added to the internal poller.
55+
NetMQRuntime is also a NetMQScheduler and SyncrhonizationContext, so any awaited function is continuing on the runtime's thread.
56+
57+
NetMQSocket should still be used only within one thread.
58+
59+
`NetMQRuntime.Run` can accept multiple tasks and also a cancellation token.
60+
`NetMQRuntime.Run` runs until all tasks are completed or cancellation token has been cancelled.

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pages:
55
- Introduction: introduction.md
66
- Concepts:
77
- Receiving and Sending: receiving-sending.md
8+
- Async/Await: async-await.md
89
- Messages: message.md
910
- Transports: transports.md
1011
- Cleanup: cleanup.md

src/NetMQ.sln

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{FF13931E-D
3939
..\docs\timer.md = ..\docs\timer.md
4040
..\docs\transports.md = ..\docs\transports.md
4141
..\docs\xpub-xsub.md = ..\docs\xpub-xsub.md
42+
..\docs\async-await.md = ..\docs\async-await.md
4243
EndProjectSection
4344
EndProject
4445
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetMQ", "NetMQ\NetMQ.csproj", "{49C06317-631D-435F-BE51-A26618FBC2C1}"

0 commit comments

Comments
 (0)