Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When the specified process doesn't has changed over a period of time,such as 2 minutes,I think the process is dead, and should be restart. How I should do using C#? Thanks
Posted
Comments
Ron Beyer 20-Jun-13 9:48am    
What is the criteria for "changed"? How do you measure that?

As you have probably noticed, Windows is monitoring processes and it is tagging the dead or dead-alike ones with "not responding" flag. The framework is giving you the means to track this flag too: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.responding.aspx[^]. Still, this is usable only for processes having UI. Services and other background processes can be monitored via the same tools by their activity as you mentioned yourself. But if you made that process you can choose better ways than that to be kept informed about it's status.
 
Share this answer
 
Comments
hjgc2001 20-Jun-13 18:52pm    
But the process I want to monitor is not made by me, i cann't get useful message from the process.What can I do? Thanks!
Zoltán Zörgő 21-Jun-13 1:57am    
What is it doing? What is the activity you could monitor?
hjgc2001 21-Jun-13 9:27am    
Because the process is important and runing in a server, and the server is nattended. So I want to monitor the process, and restart the proces if it is dead or dead-alike.
Zoltán Zörgő 21-Jun-13 13:13pm    
I got that, but what does it? If it is a service, the service host can restart it, but only if it crashes. You can monitor a third party process only based on it's activity. But only you can know what it does.
No matter what exactly you do in such situation, just killing and restarting a process would be such an abuse! Forget it, get to the root of the problem, fix the application or build a new one. Don't do evil.

—SA
 
Share this answer
 
Hi,

you can try like below.
C#
//check your conditions here

//then check process is running or not
Process[] pname = Process.GetProcessesByName("ProcessName");
if (pname.Length == 0)
  Process.Start("ProcessName");
else
{
    pname[0].CloseMainWindow();
    // Free resources associated with process.
    pname[0].Close();

    //restart the process.
    Process.Start("ProcessName");

    // Wait 2 seconds.
    Thread.Sleep(2000);
}


hope it helps
 
Share this answer
 
v2
Comments
hjgc2001 20-Jun-13 18:54pm    
Thank you very much! I have only one question: the process I want to monitor is not made by me, i cann't get useful message from the process. So how i judge the process is dead or dead-like? Thanks again!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900