0

所以我的程序从计算“XProcess”的进程数开始,并有一个计时器每 1 秒检查一次,效果很好!我将计数输入到设置变量中。然后我有一个子例程,它采用该设置变量,以及一个 IF 语句来输出 TCPClient 正在发送字符串(“一个进程”)。

好吧,问题是,我没有事件可用于子例程,所以我将它与计时器绑定在一起,每 1 秒发送一次消息。TCPClient 立即将其发送到本地地址(127.0.0.1),并将其发送到文本框。好问题!!!

它一遍又一遍地重复(“一个过程”),我可以看到为什么会发生这种情况。

因此,使用下面的代码,TCPClient 如何向 TCPServer 和 TCPSERVER 发送关于“XProcess”多少进程的通知以吐出 1 个或多个进程正在运行?(没有服务器一遍又一遍地重复字符串(整数也可以)

下面的工作,但重复有多少进程作为字符串运行。

主窗口.xaml.vb

Imports System.Windows.Threading
Imports System.Net.Sockets

Public Class MainWindow
    Private Run_ProgramRunCheck_timer As New DispatcherTimer
    Private Run_RecieveCheck_timer As New DispatcherTimer
    Dim processCount As Integer
    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        ServerStart()

        'Check for program changes every second

        ' Set interval for timer
        Run_ProgramRunCheck_timer.Interval = TimeSpan.FromMilliseconds(1000)


        'Start timer on button click
        Run_ProgramRunCheck_timer.Start()

        AddHandler Run_ProgramRunCheck_timer.Tick, AddressOf __ProgramCheck


        'Check for Message Recieve every second

        ' Set interval for timer
        Run_RecieveCheck_timer.Interval = TimeSpan.FromMilliseconds(1000)


        'Start timer on button click
        Run_RecieveCheck_timer.Start()

        AddHandler Run_RecieveCheck_timer.Tick, AddressOf _RecieveMessageConvert
        TCPClientSender()


    End Sub
    Public Sub __ProgramCheck()
        'This sub will be checked every 1 seconds for changes

        'Count number of processes
        processCount = Process.GetProcessesByName("tvnviewer").Count()
        My.Settings.TotalProcesses = processCount


    End Sub

    Public Sub TCPClientSender()
        My.Settings.TotalProcesses = 0
        If My.Settings.TotalProcesses = 1 Then
            Dim port As Int32 = 50000
            Dim client As New TcpClient("127.0.0.1", port)
            ' Translate the passed message into ASCII and store it as a Byte array.


            Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes("One Process")

            ' Get a client stream for reading and writing. 
            '  Stream stream = client.GetStream(); 
            Dim stream As NetworkStream = client.GetStream()

            ' Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length)
        End If



    End Sub

    Public Sub _RecieveMessageConvert()

        TextBlock1.Text = My.Settings.StoreSentMessage
    End Sub
End Class

TCPServer.vb

Imports System.Net
Imports System.Threading
Imports System.Net.Sockets
Imports System.IO

Public Module TCPServer

    Dim Server = New TcpListener(IPAddress.Any, 50000) ' <-- Listen on Port 50,000
    Dim Client As New TcpClient
    Private ServerThread As Thread = Nothing
    Dim Message As String = ""
    Private Threads As New List(Of Thread)

    Public Sub ServerStart()
        ServerThread = New Thread(AddressOf ConnectionListener)
        ServerThread.IsBackground = True
        ServerThread.Start()
    End Sub
    Private Sub ConnectionListener()
        Try
            Server.Start()

            While True

                Dim client As TcpClient = Server.AcceptTcpClient ' Blocks until Connection Request is Received



                Dim Reader As New StreamReader(client.GetStream())

                While Reader.Peek > -1
                    Message = Message + Convert.ToChar(Reader.Read()).ToString


                End While


                My.Settings.StoreSentMessage = Message



            End While
        Catch ex As Exception

        End Try

    End Sub




End Module
4

1 回答 1

1

你不想这样做吗:

Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(My.Settings.TotalProcesses)

我不知道当你将值设置为零然后检查下一行的值是否为 1 时,你是如何让它说“一个进程”的???

My.Settings.TotalProcesses = 0
If My.Settings.TotalProcesses = 1 Then
于 2013-10-14T19:01:33.933 回答