vb.Net-命名管道 - NamedPipeServerStream - StackOverflowException

时间:2018-05-08 17:39:28

标签: .net windows vb.net stack-overflow named-pipes

我正在尝试使用命名管道在Windows服务和WPF应用程序之间进行通信,两者都是用VB.Net编写的。 Windows服务始终在运行。

但是,在运行WPF应用程序几个小时后,会抛出List<Foo>

WPF应用程序中的

StackOverflowException是异常时托管堆栈中重复调用的唯一函数(我使用PipeServerThread()跟踪了它。)

该服务包含WinDbg,该应用包含NamedPipeClientStream

当客户端和服务器进行通信时,服务器会在几次检查后读取最终的客户端响应,然后将clientstring传递给代码中其他位置的函数。

然后冲洗,关闭和处理管道服务器。

然后再次调用NamedPipeServerStream以创建另一个管道服务器进行通信。请注意,重复调用此PipeServerThread()。

是否有人能够了解代码中最终导致PipeServerThread()发生的原因?

PipeServer:

StackOverflowException

PipeClient:

Dim PipeServers(1) As Thread
PipeServers(1) = New Thread(AddressOf PipeServerThread)
PipeServers(1).IsBackground = True
PipeServers(1).Start()

Private Sub PipeServerThread()

    Dim ps = New System.IO.Pipes.PipeSecurity

    Try
        ps.AddAccessRule(New System.IO.Pipes.PipeAccessRule("Users", System.IO.Pipes.PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Allow))
        ps.AddAccessRule(New System.IO.Pipes.PipeAccessRule("CREATOR OWNER", System.IO.Pipes.PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Allow))
        ps.AddAccessRule(New System.IO.Pipes.PipeAccessRule("SYSTEM", System.IO.Pipes.PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Allow))

        Dim pipeserver = New System.IO.Pipes.NamedPipeServerStream("testpipe", Pipes.PipeDirection.InOut, 10, Pipes.PipeTransmissionMode.Byte, Pipes.PipeOptions.None, 4024, 4024, ps)

        Debug.WriteLine("Pipeserver waiting for connection")
        pipeserver.WaitForConnection()
        Try
            'Debug.WriteLine("Pipeserver now within Try block.")
            Dim ss As New StreamString(pipeserver)
            'This server writes to pipe immediately on connection
            Debug.WriteLine("Server now writing to Pipe")
            ss.WriteString("Hello Client. Who are you?")                                            'Action 1 - server writes to Pipe

            'Response from client is read
            Dim ClientName As String = ss.ReadString                                                'Action 4 - server reads from Pipe
            Debug.WriteLine("Client's name is: " & ClientName)
            If ClientName = "PipeWriterService" Then
                'The client is the PipeWriterService
                'Tell it what state is being requested
                ss.WriteString(RequestedCommState)                                                  'Action 5 (if) - server writes to Pipe
            Else
                'The client is someting else
                ss.WriteString("wrong client")                                                      'Action 5 (else) - server writes to Pipe
            End If

            Dim ClientResponse As String = ss.ReadString()
            Debug.WriteLine("Server has read client Response as: " & ClientResponse)                'Action 8 - server reads from Pipe
            DeviceInfo.Dispatcher.BeginInvoke(Windows.Threading.DispatcherPriority.Normal, New OneArgDelegate3(AddressOf ProcessPipeString), ClientResponse)
        Catch ex As Exception
            Debug.WriteLine("Error is Server Try block: " & ex.Message)
        End Try
        pipeserver.Flush()
        pipeserver.Close()                                                                          'Action 9 - server closes Pipe
        pipeserver.Dispose()
        Debug.WriteLine("PipeServer now closed")
    Catch ex As Exception

    End Try

    Call PipeServerThread()
End Sub

1 个答案:

答案 0 :(得分:3)

目前您可能已经解决了问题。

无论如何,问题在于在PipeServerThread()上的递归调用。每次调用它时,堆栈的大小都会增加,以容纳局部变量和函数的返回地址。

您必须摆脱递归调用的作用,并将函数体置于循环中。