none
named pipe and winforms

    Question

  • I am trying to convert the Console application examples 

    https://docs.microsoft.com/en-us/dotnet/api/system.io.pipes.namedpipeserverstream?view=netframework-4.0

    https://docs.microsoft.com/en-us/dotnet/api/system.io.pipes.namedpipeclientstream?view=netframework-4.0

    to WinForm application

    but with the client sending and the server receiving

    When I run the code, the 5 letters sent by the client is not displayed in the text box on the server form until I close down the client program.

    What is causing this and how do I fix it?

    this is the code for the server

    	Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    		pipeServer = New NamedPipeServerStream("testpipe", PipeDirection.InOut)
    		TextBox1.Text = "NamedPipeServerStream object created."
    		TextBox1.Refresh()
    
    		TextBox1.Text = TextBox1.Text & vbCrLf & "Waiting for client connection..."
    		TextBox1.Refresh()
    
    		pipeServer.WaitForConnection()
    
    		TextBox1.Text = TextBox1.Text & vbCrLf & "Client connected."
    		TextBox1.Refresh()
    
    
    		Dim sr As New StreamReader(pipeServer)
    		Dim myString As String
    		myString = sr.ReadLine
    		TextBox1.Text = TextBox1.Text & vbCrLf & myString
    		TextBox1.Refresh()
    
    		While Not myString Is Nothing
    			myString = sr.ReadLine
    			TextBox1.Text = TextBox1.Text & vbCrLf & myString
    			TextBox1.Refresh()
    		End While
    
    
    		TextBox1.Text = TextBox1.Text & vbCrLf & "end"
    		TextBox1.Refresh()
    
    	End Sub

    this is the code for the client

    	Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    		Dim pipeClient As New NamedPipeClientStream("localhost", _
    		 "testpipe", PipeDirection.InOut, PipeOptions.None)
    
    		' Connect to the pipe or wait until the pipe is available.
    		TextBox1.Text = "Attempting to connect to the pipe..."
    		TextBox1.Refresh()
    
    		pipeClient.Connect()
    
    		TextBox1.Text = TextBox1.Text & vbCrLf & "Connected to the pipe."
    		TextBox1.Text = TextBox1.Text & vbCrLf & "There are currently " & pipeClient.NumberOfServerInstances & " pipe server instances open."
    		TextBox1.Refresh()
    
    
    		Dim sw As New StreamWriter(pipeClient)
    		sw.AutoFlush = True
    		Dim myBuffer() As Char = {"a", "b", "c", "d", "e"}
    		sw.Write(myBuffer)
    
    
    		TextBox1.Text = TextBox1.Text & vbCrLf & myBuffer
    		TextBox1.Text = TextBox1.Text & vbCrLf & "end"
    
    
    	End Sub
    

    Thursday, May 23, 2019 3:13 PM

All replies

  • Try adding ‘Using’ to pipes and streams. This is the simplified sender:

    Using pipeClient As New NamedPipeClientStream("localhost", "testpipe", PipeDirection.InOut, PipeOptions.None)

     

        pipeClient.Connect()

     

        Using sw As New StreamWriter(pipeClient)

            sw.AutoFlush = True

            Dim myBuffer() As Char = {"a"c, "b"c, "c"c, "d"c, "e"c}

            sw.Write(myBuffer)

        End Using

     

    End Using

     

    Add the same directive to receiver.

    Thursday, May 23, 2019 4:36 PM
  • Thanks.

    The error was that I need sw.WriteLine(myBuffer) .


    Thursday, May 23, 2019 6:43 PM
  • Hi,

    If your issue is solved,please mark the helpful as answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 24, 2019 1:38 AM
    Moderator