Callback on Named Pipe question

D

dgk

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.
 
D

dgk

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top