This is my problem... I have some code that starts a Process and
returns it to a variable... (prcBat)
At any time while that process is running... I want to be able to Kill
it by pressing a command button in the application.
So the code goes something like this:
psiBat = New ProcessStartInfo(arrRun.Item(0)(1))
psiBat.WindowStyle = ProcessWindowStyle.Hidden
psiBat.CreateNoWindow = True
prcBat = Process.Start(psiBat)
prcBat.WaitForExit()
And I have a command button that has this...
prcBat.Kill()
prcBat = Nothing
Now... my problem is this... some of the files that will be run by
this process are executables... when I start an exectable like so:
prcBat = Process.Start(psiBat)
....it immediately returns the variable prcBat and I can kill it right
away.
BUT... if the file I'm trying to run is a data file (for example, an
..xls file) then the line:
prcBat = Process.Start(psiBat)
....opens the Excel file (which contains its own code) BUT it doesn't
return the variable prcBat... it just waits at that line of code until
the Excel application is finished. (which makes the "WaitForExit" line
pretty much useless, since it waits for completion of the Process
before moving to the next line anyway)
So this does me no good at all, since I may want to kill the process
before it's finished. When I try to kill it using the command button,
if I'm running an executable, it works just fine, but if I ran an
Excel file, then it throws an exception because prcBat doesn't exist
yet (because the Start line hasn't returned the Process to the
variable yet).
So what do I do? How do I run a data file in a process and immediately
return the Process so I can kill it if needed? My real problem here is
with the inherent behavior in the Process.Start method. It doesn't
return its results to the variable immediately if it's running a data
file... it only does so if it's running an executable... which makes
this Method half-worthless. (what use is there returning the Process
to a variable if it won't happen until after the process is already
done running...???)
Please let me know of any ideas/suggestion you may have to get around
this. I may have to build my own function to replace the Process.Start
method... I can probably use a combination of the Shell command
(figure out which application opens the data file, if a data file is
specified) and pass the Process ID from the Shell command back to a
variable... but I'd rather find a more straightforward workaround if
possible.
WATYF