Chapter 10
internal static Boolean WriteToComPort( string textToWrite )
{
Boolean success;
if (myComPort.IsOpen)
{
// Write the passed String to the port.
myComPort.Write(textToWrite);
success = true;
}
return success;
}
After calling BeginInvoke, the application’s thread is free to do other things
while the write operation is in progress. When the write operation completes,
the callback routine executes. The callback routine can perform any actions
required after the write operation completes.
Friend Sub WriteCompleted(ByVal ar As IAsyncResult)
Dim msg As String
Dim deleg As WriteToComPortDelegate
Dim success As Boolean
' To obtain the msg value passed to the BeginInvoke method,
' cast BeginInvoke's IAsyncResult value to an AsyncResult object
' and get the object's AsyncDelegate property.
deleg = DirectCast(DirectCast(ar, AsyncResult).AsyncDelegate, _
WriteToComPortDelegate)
' The msg value is in the AsyncState property.
msg = = DirectCast(ar.AsyncState, String)
' The EndInvoke method returns the value returned by WriteToComPort.
success = WriteToComPortDelegate1.EndInvoke(ar)
Console.WriteLine("Write operation began: " & msg)
Console.WriteLine("Write operation succeeded: " & success)
End Sub