Glossary Item Box
Asynchronous operations are useful because they occur without waiting for the action to complete before executing the next line of code. Such a technique is useful when an operation is time consuming and other code needs to execute without waiting for the operation to complete. The following example demonstrates receiving data asynchronously using the BeginReceive method. Asynchronous use works similarly for connecting (BeginConnect) and sending data (EndSend).
To receive data asynchronously.
[Visual Basic, C#]
[C#] // Connect to an echo port. tcp1.Connect("myserver", 7); // Send some data tcp1.Send("hello"); // Begin to asynchronously receive data...the EndReceive event will be raised when completed byte[] buffer = new byte[tcp1.ReceiveBufferSize]; tcp1.BeginReceive(buffer); [Visual Basic] ' Connect to an echo port. Tcp1.Connect("myserver", 7) ' Send some data Tcp1.Send("hello") ' Begin to asynchronously receive data...the EndReceive event will be raised when completed Dim buffer(Tcp1.ReceiveBufferSize) As Byte Tcp1.BeginReceive(buffer)
[Visual Basic, C#]
[C#] private void tcp1_EndReceive(object sender, Dart.PowerTCP.SslSockets.SegmentEventArgs e) { // Receive operation is complete...check for any exceptions if(e.Exception == null) { // Display the data received. Debug.WriteLine(e.Segment.ToString()); } } [Visual Basic] Private Sub Tcp1_EndReceive(ByVal sender As Object, ByVal e As Dart.PowerTCP.SslSockets.SegmentEventArgs) Handles Tcp1.EndReceive ' Receive operation is complete...check for any exceptions If e.Exception Is Nothing Then ' Display the data received. Debug.WriteLine(e.Segment.ToString()) End If End Sub
Send comments on this topic.
Documentation version 1.1.2.0.
© 2008 Dart Communications. All rights reserved.