472,146 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Kill a Process before it's finished running...

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)
And I have a command button that has this...

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
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

Nov 20 '05 #1
0 2066

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by John Lin | last post: by
6 posts views Thread by Bob Swerdlow | last post: by
12 posts views Thread by Steve | last post: by
6 posts views Thread by Matthew Wieder | last post: by
3 posts views Thread by elrondrules | last post: by
4 posts views Thread by Richard Rossel | last post: by
18 posts views Thread by =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post: by
reply views Thread by Saiars | last post: by

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.