Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
878 views
in Technique[技术] by (71.8m points)

c# - How to check if process is not responding?

Is there any possibility to check from code if another process is not responding?

The problem is even if the app is crashed and on the Manager's list is marked as 'Not Responding', the Process.Responding property is still returning 'true'.

The 'Exited' event and function 'WaitForExit' are do any action if the process is- what is clear- exited. So it's not the point.

Problem in two words; I need to know that the application is crashed. How to check it from the code?

Thank you for your time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no general solution to this problem.

It is impossible to tell if a particular process is hanging or not, because the term "hanging" is entirely dependent upon the context of the process that is executing.

The hanging process will always do what it was coded to do. The developer may have coded it badly, but Windows can't make assumptions about what is right/wrong.

Possible ideas to try might be:

  1. The Process.Responding call will indicate whether or not a process that is executing a windows message loop is responding.

  2. One possible solution for the more general case might be to poll the memory usage for the process at intervals and if it doesn't change after enough time, assume that it is hung. You could do this with Process.WorkingSet64. However, I expect that this would cause a number of false positives - stable processes that are not processing anything might appear to be hanging. It would also cause false negatives where a hanging process that had a memory leak would appear to be doing something useful, when in fact it was stuck in a loop.

  3. If the process writes to the StandardError/StandardOutput streams (as many console applications do), then you could try listening for such output: Process.BeginOutputReadLine and Process.BeginErrorReadLine. If there is no such output within a given period, you might deduce that it has hung.

But you're not going to find anything that works in the general case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...