Skip to content

Asynchronous usage with async and await

Thijs Elenbaas edited this page Jan 6, 2019 · 3 revisions

Much of the functionality of SimplSockets and SimplMessage is near instantaneous and in these cases asynchronous usage does not bring much benefit, but in some cases the application will need to wait, e.g. on establishing the connection or waiting for a reply. In these cases the libraries supply an Async variant of the functions.

Await connecting to the server

You can await a connection having been made from both the server and client:

var connectedClient = await _server.WaitForNewClientAsync();
await _client.WaitForConnectionAsync();

Awaiting an reply

You can await a reply from the server for SimplSockets and SimplMessage.

var receivedMessage = await client.SendReceiveAsync(messageToSend);
var receivedObject = await _client.SendReceiveAsync<ClassA, ClassA>(objectToSend);

you can also include a timeout (in ms) after which the function will stop waiting for a reply

var receivedObject = await _client.SendReceiveAsync<ClassA, ClassA>(objectToSend,1000);

Example

These features are shown in more detail in this example