Visual Basic线程错误

时间:2015-02-13 18:56:26

标签: vb.net

我目前正在学习如何使用VB。我启动的一个项目是创建一个应用程序,允许用户通过单击按钮(在这种情况下为两个)添加打印机。目前我遇到了一个我不太懂的线程错误。其他人当然也有类似的问题,但似乎没有一个对我的案例有帮助。我感谢任何帮助!

Imports System.Threading

Public Class Form1
'Form code from sample project

    Private Results As String

    Private Delegate Sub delUpdate()

   'Sends command (workroom_color) to txtCommand
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Command1 As String
        Command1 = ("rundll32 printui.dll PrintUIEntry /in /n \\selene\3Points_workroom_color")
        txtCommand1.Text = (Command1)
    End Sub

    'Sends command (for workroom iR5055) to txtCommand
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Command2 As String
        Command2 = ("rundll32 printui.dll PrintUIEntry /in /n \\selene\3Points Workroom Cannon iR5055")
        txtCommand2.Text = Command2
    End Sub

    'Sends command (for workroom iR5055/5065) to txtCommand
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Command3 As String
        Command3 = ("rundll32 printui.dll PrintUIEntry /in /n \\selene\3points_Workroom_Canon_iR5055")
        txtCommand3.Text = (Command3)
    End Sub


    'Runs txtCommand.Text
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
        CMDThread.Start()
    End Sub

    Private Sub CMDAutomate()
        Dim myprocess As New Process
        Dim StartInfo As New System.Diagnostics.ProcessStartInfo
        StartInfo.FileName = "cmd" 'starts cmd window
        StartInfo.RedirectStandardInput = True
        StartInfo.RedirectStandardOutput = True
        StartInfo.UseShellExecute = False 'required to redirect
        StartInfo.CreateNoWindow = True 'creates no cmd window
        myprocess.StartInfo = StartInfo
        myprocess.Start()
        Dim SR As System.IO.StreamReader = myprocess.StandardOutput
        Dim SW As System.IO.StreamWriter = myprocess.StandardInput
        SW.WriteLine(txtCommand1.Text, txtCommand2.Text, txtCommand3.Text) 'the command you wish to run.....
        SW.WriteLine("exit") 'exits command prompt window
        Results = SR.ReadToEnd 'returns results of the command window
        SW.Close()
        SR.Close()
    End Sub


End Class

2 个答案:

答案 0 :(得分:0)

我猜你是从新线程访问UI,这会导致交叉线程异常。您必须通过调用Control.BeginInvokeControl.Invoke来编组回UI线程,具体取决于您是否希望调用是异步的。例如:

txtCommand1.Invoke(Sub(results) txtCommand1.Text = results, Results)

答案 1 :(得分:0)

使用类对象,您可以在其中存储可在内部使用的数据。

Public Class ThreadingObject
 Public param1 As String
 Public param2 As String
 Public param3 As String
 Public Event Results(data As String)
 Public Sub CMDAutomate()
    Dim myprocess As New Process
    Dim StartInfo As New System.Diagnostics.ProcessStartInfo
    StartInfo.FileName = "cmd" 'starts cmd window
    StartInfo.RedirectStandardInput = True
    StartInfo.RedirectStandardOutput = True
    StartInfo.UseShellExecute = False 'required to redirect
    StartInfo.CreateNoWindow = True 'creates no cmd window
    myprocess.StartInfo = StartInfo
    myprocess.Start()
    Dim SR As System.IO.StreamReader = myprocess.StandardOutput
    Dim SW As System.IO.StreamWriter = myprocess.StandardInput
    SW.WriteLine(param1, param2, param3) 'the command you wish to run.....
    SW.WriteLine("exit") 'exits command prompt window
    RaiseEvents Results(SR.ReadToEnd) 'returns results of the command window
    SW.Close()
    SR.Close()
 End Sub
End Class

用法:

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click

 Dim thrObj As New ThreadingObject 
 thrObj.param1 = textbox1.Text 
 thrObj.param2 = textbox2.Text 
 thrObj.param3 = textbox3.Text
 AddHandler thrObj.Results, AddressOf ReceivedEvents
 Dim CMDThread As New Threading.Thread(AddressOf thrObj.CMDAutomate)
 CMDThread.Start()
End Sub

Private Sub ReceivedEvents(data As String)
 'this still comes from another thread
 Me.Invoke(Sub() 
            'access controls or form in here
             txtResults.Text = data 'example
           End Sub)
End Sub