473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to start new process from inside BackgroundWorker?

rauty
16
Hi all,

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

"TargetInvocationException was unhandled"


The culprit seems to be this guy:
Expand|Select|Wrap|Line Numbers
  1. 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 BackgroundWorker 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!
Apr 2 '09 #1
1 3185
rauty
16
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
Expand|Select|Wrap|Line Numbers
  1. 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 BackgroundWorker):
Expand|Select|Wrap|Line Numbers
  1. Try
  2.             ' Call MATLAB routine (grain.exe) to calculate grain
  3.             Dim GrainProcess As New Process()
  4.             Dim OutputString As String
  5.             Dim GrainInfo() As String
  6.  
  7.             ReDim Preserve GrainArray(GrainCount)
  8.             ReDim Preserve MatlabLstar(GrainCount)
  9.             ChDir(MatlabDir)
  10.             With GrainProcess
  11.               With .StartInfo
  12.                 .UseShellExecute = False
  13.                 .RedirectStandardOutput = True
  14.                 .RedirectStandardError = True
  15.                 .CreateNoWindow = True
  16.                 .FileName = "grain.exe"
  17.                 .Arguments() = ImgSaveMode & " RawImg.bmp " & ICCfile
  18.               End With
  19.               .Start()
  20.               OutputString = Trim(.StandardOutput.ReadToEnd)
  21.               ' Clean up the output (get rid of spaces, LF, etc.)
  22.               GrainInfo = Split(OutputString, vbLf)
  23.               Array.Copy(GrainInfo, 2, GrainInfo, 1, 1)
  24.               ReDim Preserve GrainInfo(1)
  25.               GrainInfo(1) = Trim(GrainInfo(1))
  26.               GrainArray(GrainCount) = GrainInfo(0)
  27.               MatlabLstar(GrainCount) = GrainInfo(1)
  28.               .WaitForExit()
  29.             End With
  30.           Catch ex As Exception
  31.             MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Error Accessing grain.exe")
  32.             ex = Nothing
  33.           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 BackgroundWorker, but can't start a new process. Any help with this would be greatly appreciated!
Apr 3 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: JohnnyGr | last post by:
I have heard theres a new way to start threads with parameters in framework 2.0, does anyone know how to do that? this is what i need to do... Start a thread that executes some stuff, in this...
5
by: Rob R. Ainscough | last post by:
I'm using a BackgroundWorker to perform a file download from an ftp site. Per good code design practices where I separate my UI code from my core logic code (in this case my Download file method in...
6
by: Kyote | last post by:
I'm trying to make, what I thought, would be a simple application. This application will help me organize files I have a lot of, on my computer(ebooks, pictures, etc..). I'm currently dealing with...
4
by: Diego F. | last post by:
Hello. I have a process that can take a long time and I'm trying to put an animated gif while the user is waiting. I added a panel with a picturebox with the image inside. The panel is not...
5
by: Manikandan | last post by:
Hi, My application is processing a database(selecting data using execute reader) It takes much time. I want to show user the application is running, showing some information in the form I tried...
4
by: Marcolino | last post by:
Hi All, I'm using this code provided by Michael to run Async process: ...
32
by: John Wright | last post by:
I have a long process I run that needs to be complete before the user can continue. I can change the cursor to an hourglass, but I want to update the Status Strip on the bottom during the process. ...
4
by: =?Utf-8?B?U3ZlbiBXLg==?= | last post by:
Hi Newsgroup, I am developing a C# Windows Forms application that launches processes within a background worker.. The processes seem to have a memory limit of about 278mb. Some proccesses...
4
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, I have winForm that needs update Panel (add controls to Panel of SplitContainer dynamically). I have a Backgroundworker, and DoWork event I want to add controls but I get this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.