Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:07:00+00:00 2026-06-14T23:07:00+00:00

I use Process.Kill() to kill a process. Like this: if( !process.WaitForExit( 5000 ) )

  • 0

I use Process.Kill() to kill a process. Like this:

if( !process.WaitForExit( 5000 ) ) {
   process.Kill();
}

and sometimes the process will exit right in between the lines, so control will get inside if and then Kill will yield an exception:

System.InvalidOperationException
Cannot process request because the process (ProcessIdHere) has exited.
at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.Kill()
//my code here

Now wrapping the code into try-catch doesn’t seem to be a good idea because InvalidOperationException can be called for other reasons.

Is there a way to kill a process without getting an exception in the described scenario?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:07:01+00:00Added an answer on June 14, 2026 at 11:07 pm

    You could P/Invoke TerminateProcess passing it Process.Handle. Then manually evaluating the cause of it (GetLastError()). Which is roughly, what Process.Kill() does internally.

    But note that TerminateProcess is asynchronous. So you’d have to wait on the process handle to be sure it is done. Using Process.Kill() does that for your.

    Update: Correction, Process.Kill() also runs asynchronously. So you’ll have to use WaitForExit() to wait for termination to complete – if you care.

    Frankly, I wouldn’t bother. Of course there is always the (remote?) chance that some “arbitrary” InvalidOperationExcepion bubbles up out of that line of code, that is not related to the process no longer being there or the Process object to be in an invalid state, but in reality I think you can just go with the try/catch around the Kill.

    In addition, depending on your application, you could consider logging this kill anyway, since it seems some sort of last resort measure. In that case log the actual InvalidOperationException with it. If things go strange, you at least have your logs to check why the Kill failed.

    Having all that said, you might also want to consider catching / handling Win32Exception for the same reasons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to kill windows explorer's process (explorer.exe), for that lets say i use
I would like to know how to kill a process that has started up.
I use process explorer (which is a microsoft tool) on windows XP, and often
How can I use Process.Start(), but have the launched process not in the same
When should I use process-wide dictionary and when should my process state be in
What is the use of process option in Analysis service Database,Cube,Dimension seperately?If we process
Which one to use for process monitoring and why?
i have application who use specific process during running and i want to sample
I use python to process web page. I download the souce code of the
Is it possible to use getopts to process multiple options together? For example, myscript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.