How to start new process from inside BackgroundWorker?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rauty
    New Member
    • Nov 2008
    • 16

    How to start new process from inside BackgroundWorker?

    Hi all,

    I have a situation where I'm using BackgroundWorke r to run a long process in the background. However, whenever it runs I keep getting the following error:

    "TargetInvocati onException was unhandled"


    The culprit seems to be this guy:
    Code:
    Dim GrainProcess As New Process()
    In my DoWork sub I'm calling a sub in another module. The above line is in the middle of that other sub. I'm using Process.Start to run a Matlab executable file (not written by me) that processes an image and spits out a number.

    I tried putting the Dim statement outside the sub, but then it just gives me the same error a couple of lines down when I try to set the .StartInfo properties.

    Does the "was unhandled" part of the above exception mean that I need to provide some kind of error checking (maybe a try...catch), or does it mean that I'm not allowed to do a Process.Start from within a BackgroundWorke r thread?

    I'm pretty new to threading in general, so any help would be greatly appreciated. Code examples would be very helpful, since I'm not a programmer by nature and can't always make heads or tails out of what I read in the Microsoft Documentation.

    Please also let me know if you need any additional info.

    Thanks for your time!
  • rauty
    New Member
    • Nov 2008
    • 16

    #2
    Well, I tried putting the Process.Start routine in a Try...Catch block, but that didn't make any difference. One thing is, when the error occurs, the line
    Code:
    OutputString = Trim(.StandardOutput.ReadToEnd)
    is highlighted in light gray, along with the Catch section. Here is all of the code I use to run the executable (from within the BackgroundWorke r):
    Code:
    Try
                ' Call MATLAB routine (grain.exe) to calculate grain
                Dim GrainProcess As New Process()
                Dim OutputString As String
                Dim GrainInfo() As String
    
                ReDim Preserve GrainArray(GrainCount)
                ReDim Preserve MatlabLstar(GrainCount)
                ChDir(MatlabDir)
                With GrainProcess
                  With .StartInfo
                    .UseShellExecute = False
                    .RedirectStandardOutput = True
                    .RedirectStandardError = True
                    .CreateNoWindow = True
                    .FileName = "grain.exe"
                    .Arguments() = ImgSaveMode & " RawImg.bmp " & ICCfile
                  End With
                  .Start()
                  OutputString = Trim(.StandardOutput.ReadToEnd)
                  ' Clean up the output (get rid of spaces, LF, etc.)
                  GrainInfo = Split(OutputString, vbLf)
                  Array.Copy(GrainInfo, 2, GrainInfo, 1, 1)
                  ReDim Preserve GrainInfo(1)
                  GrainInfo(1) = Trim(GrainInfo(1))
                  GrainArray(GrainCount) = GrainInfo(0)
                  MatlabLstar(GrainCount) = GrainInfo(1)
                  .WaitForExit()
                End With
              Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Error Accessing grain.exe")
                ex = Nothing
              End Try
    See below for a screen capture with highlighted areas:

    For some reason I can make calls to functions within a DLL from the BackgroundWorke r, but can't start a new process. Any help with this would be greatly appreciated!

    Comment

    Working...