Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Recommended way to wait for a process exit

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
andrew - 26 Jul 2007 17:47 GMT
Hi,

I have this situation:
A worker thread creates and starts a process and then it needs to wait for
it to exit.
Now, its possible that the thread to be aborted using the Thread.Abort().
I know this is not the recommended way but for some reasons I cannot change
this behaviour now.

The problem is how to wait for the process exit taking into account the
thread abort.
if the thread is using
process.WaitForExit();
the Abort() call wont return until the process exits.
this is because the WaitForExit() I think what it does is to "make an
unmanaged call into the operating system that has blocked the thread in
unmanaged code" (quoted from MSDN).
http://msdn2.microsoft.com/en-us/library/74169f59.aspx
(Blocking issues)

An idea would be to do this:
WaitHandle waitForExit = new WaitHandle();
waitForExit.Handle = process.Handle;
waitFoExit.WaitOne();

the MSDN says at Handle Process property that it could be used to intialize
a WaitHandle and the "Blocking Issues" section suggest that
WaitHandle.WaitOne() is a recommended way to wait in order for the
Thread.Abort() to interrupt the thread.

what do you think ?
andrew - 26 Jul 2007 17:50 GMT
in fact it should be like this:

WaitHandle[] waitForExit = new WaitHandle[1];
waitForExit[0].Handle = process.Handle;
waitForExit[0].WaitOne();

since WaitHandle ctor is protected.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.