From: dgk on
I trying to use a named pipe but can't figure out how to get the
callback to work:

Module Module1
Private ps As System.IO.Pipes.NamedPipeServerStream
Public Sub main()
Application.EnableVisualStyles()
Try
ps = New System.IO.Pipes.NamedPipeServerStream("Test")
ps.BeginWaitForConnection(AddressOf HandleConnection, "Test")
Catch ex As Exception
' pipe already exists
End Try
Application.Run(Form1)
End Sub


Public Sub HandleConnection(ByVal ar As System.IAsyncResult)
ps.EndWaitForConnection(ar)
Using sr As New System.IO.StreamReader(ps)
Dim message As String = ""
Do
message = sr.ReadLine
Console.WriteLine(message)
Loop While message IsNot Nothing
End Using
End Sub
End Module


I don't understand what the second parameter (object) is doing on the
BeginWaitForConnection. Maybe because of that, and maybe not,
HandleConnection doesn't ever fire, even when another program does
open the pipe Test. Nor do I understand why I need to call
EndWaitForConnection (maybe I don't need to?), or what to do with the
IAsyncResut.

Any info greatly appreciated.
From: dgk on
On Thu, 27 Dec 2007 10:41:34 -0500, dgk <dgk(a)somewhere.com> wrote:

>I trying to use a named pipe but can't figure out how to get the
>callback to work:
>
>Module Module1
> Private ps As System.IO.Pipes.NamedPipeServerStream
> Public Sub main()
> Application.EnableVisualStyles()
> Try
> ps = New System.IO.Pipes.NamedPipeServerStream("Test")
> ps.BeginWaitForConnection(AddressOf HandleConnection, "Test")
> Catch ex As Exception
> ' pipe already exists
> End Try
> Application.Run(Form1)
> End Sub
>
>
> Public Sub HandleConnection(ByVal ar As System.IAsyncResult)
> ps.EndWaitForConnection(ar)
> Using sr As New System.IO.StreamReader(ps)
> Dim message As String = ""
> Do
> message = sr.ReadLine
> Console.WriteLine(message)
> Loop While message IsNot Nothing
> End Using
> End Sub
>End Module
>
>
>I don't understand what the second parameter (object) is doing on the
>BeginWaitForConnection. Maybe because of that, and maybe not,
>HandleConnection doesn't ever fire, even when another program does
>open the pipe Test. Nor do I understand why I need to call
>EndWaitForConnection (maybe I don't need to?), or what to do with the
>IAsyncResut.
>
>Any info greatly appreciated.


Ok, I get to answer my own question. If you want to use async methods
with named pipes, you need to open the pipe as async:

ps = New System.IO.Pipes.NamedPipeServerStream("Test",
IO.Pipes.PipeDirection.InOut, 3,
IO.Pipes.PipeTransmissionMode.Message,
IO.Pipes.PipeOptions.Asynchronous)