如何确保将数据发送到正确的流程?

时间:2019-04-10 13:58:21

标签: vb.net process sendkeys

我有一个在Windows CE上运行的条形码读取器,该读取器具有一个应用程序,可将条形码发送到使用TCP / IP协议在PC上运行的应用程序。而且我需要将此应用程序中接收到的数据发送到任何MS ACCESS * 32进程,该进程是活动应用程序,这意味着它是作为文本框中输入光标的应用程序,等待活动窗口中的输入。

但是当我使用Process.GetCurrentProcess()时,它没有返回MS ACCESS作为当前进程,而是返回了vb.net应用程序,即使该应用程序在后台。

MS ACCESS应用程序位于前台,如果我们使用键盘输入,它会将输入发送到MS Access中表单的文本框中。

我不明白为什么Process.GetCurrentProcess()不将MS Access作为当前进程返回。

我不想使用进程ID,因为运行的MS Access应用程序可能不止一个,并且希望确保将数据发送到前台的数据。


此子例程接收扫描的条形码,并检查MS ACCESS进程是否处于活动状态。如果是这样,它将向其发送数据。否则,它将告诉用户使Access成为活动应用程序,并在15秒后重试向其发送数据。 如果成功,它将信息发送到条形码扫描仪。如果不是,它将向条形码扫描仪发送错误。

Private Sub HandleClientComm(ByVal client As Object)
        Try
            Dim tcpClient As TcpClient = DirectCast(client, TcpClient)
            Dim clientStream As NetworkStream = tcpClient.GetStream()

            Dim buffer As Byte() = New Byte(4095) {}
            Dim strCodeBarre As String = ""
            Dim bytesRead As Integer

            While True
                Dim strServerResponse As String = ""
                Dim Chrono As New Stopwatch
                Dim bTryAgain As Boolean = False

                Try
                    bytesRead = 0
                    bytesRead = clientStream.Read(buffer, 0, 4096)

                    strCodeBarre = Encoding.Unicode.GetString(buffer, 0, bytesRead)
                    If TextBox1.Text <> "" Then
                        TextBox1.Text &= vbNewLine
                    End If
                    TextBox1.Text &= strCodeBarre & " R"

                    strCodeBarre = Encoding.Unicode.GetString(buffer, 0, bytesRead)

                    Do
                        Dim currentProcess As Process = Process.GetCurrentProcess()
                        Dim NomProcess As String = currentProcess.ProcessName
                        Chrono.Reset()
                        While NomProcess <> "MS ACCESS *32" AndAlso Chrono.Elapsed.TotalSeconds < 15
                            MsgBox("Assurez-vous que l'application Expédition soit l'application active, vous avez 15 secondes pour le faire après avoir cliqué sur OK ", , "Process actif est " + NomProcess)

                            Chrono.Start()
                            Do
                                ' attendre 15 secondes     
                            Loop While Chrono.Elapsed.TotalSeconds < 15
                            ' vérifier quel est le process actif
                            currentProcess = Process.GetCurrentProcess()
                            NomProcess = currentProcess.ProcessName
                            If NomProcess <> "MS ACCESS *32" Then
                                Chrono.Reset()
                                Chrono.Start()
                            End If

                        End While
                        Chrono.Reset()
                        Chrono.Start()

                        Do
                        Loop While Not currentProcess.WaitForInputIdle(1000)
                        Chrono.Stop()
                        SendKeys.SendWait(strCodeBarre & vbCr)
                        TextBox1.Text &= "E" & Chrono.Elapsed.TotalSeconds
                        strServerResponse = "1 Barcode envoyé " + strCodeBarre
                        bTryAgain = True
                    Loop While bTryAgain

                    'strServerResponse = "1OK"
                Catch ex As Exception
                    strServerResponse = "0ERREUR" & vbNewLine & _
                                        "Une erreur s'est produite sur le PC de l'expédition." & vbNewLine & _
                                        "Veuillez réessayer."
                End Try

                If strServerResponse(0) = "1"c Then
                    TextBox1.Text &= "S"
                Else
                    TextBox1.Text &= "F"
                End If

                Dim sendBytes As Byte() = Encoding.Unicode.GetBytes(strServerResponse)
                clientStream.Write(sendBytes, 0, sendBytes.Length)
            End While

            tcpClient.Close()
        Catch
            ' Intentionnellement vide
        End Try
    End Sub

```

---------------------

I expected that Process.GetCurrentProcess() would be MS Access.

Process.GetCurrentProcess() it is not returning MS ACCESS as the current process it's returning the vb.net application instead even if that application is in the background.

0 个答案:

没有答案