Question Process.Start Threading

abakshi

New member
Joined
Dec 8, 2008
Messages
1
Programming Experience
10+
I've been using Process.Start for a bit now, and it's working pretty well, but now I have a slight issue.

I'm using it in a WinForms program to run a command-line app (hidden) that at a certain point waits for the user to plug in a device. It outputs a line saying "Plug in device," and then waits. I'm processing each line of output, so when this line comes in, my program is supposed to show a graphical form telling the user to plug in the device, then once the command-line app reaches the next step, my program should close the graphical form.

The problem is that while the command-line app waits for the user to plug in the device, my program locks up, and only when the command-line apps' wait process times out does my program respond-- instantly opening and then closing the graphical form and showing a timeout message.

So I figure I need some way of multi-threading this, to keep the UI responsive-- how can I do that?

Here's the relevant part of the code:

VB.NET:
        Dim clsProcess As New System.Diagnostics.Process() 
        clsProcess.StartInfo.UseShellExecute = False 
        clsProcess.StartInfo.RedirectStandardOutput = True 
        clsProcess.StartInfo.RedirectStandardError = True 
        clsProcess.StartInfo.FileName = cPath & "loader.exe" 
        clsProcess.StartInfo.Arguments = "-mode download" & comPort 
        clsProcess.StartInfo.WorkingDirectory = cPath 
        clsProcess.StartInfo.CreateNoWindow = True 
        clsProcess.Start() 

        Dim _frmPlugInDevice As New frmPlugInDevice 'device plug-in popup 

        Dim WaitingForPlugIn As Boolean 'waiting for device plug-in? 

        While (clsProcess.HasExited = False) 
            Dim sLine As String = clsProcess.StandardOutput.ReadLine 
            If (Not String.IsNullOrEmpty(sLine)) Then 
                txtStatus.Text &= sLine & vbCrLf 

                'process command-line output 

                If sLine.Contains("Plug in device") Then 
                    _frmPlugInDevice.Show() 'show plug-in dialog 
                    WaitingForPlugIn = True 'waiting for plug-in 
                End If 
                If sLine.Contains("Error") Then 
                    If WaitingForPlugIn = True Then 
                        _frmPlugInDevice.Hide() 'hide plug-in dialog if error 
                        WaitingForPlugIn = False 
                    End If 
                End If 
                If sLine.Contains("Error establishing connection") Then 
                    'alert user, show retry button 
                    If WaitingForPlugIn = True Then 
                        _frmPlugInDevice.Hide() 'hide PlugIn dialog if error 
                        WaitingForPlugIn = False 
                    End If 
                    Return False 
                End If 

            End If 

            Application.DoEvents() 
        End While

Any ideas?
 
I would be running all of this in a Background Worker's DoWork event. You'll need to use a delegate to show the "Plug in device" window because I'm sure you'll want that form opened from the UI thread and not the background thread.
 
Back
Top