3

I want to create a watchdog in c# in which the user selects a process by his pid a then the application watch he consumes of ram and CPU, after, if that application pass over the min consume then restart the process so his CPU and ram go again to 0.

my problem is when I want to restart the process, because I can get the process by his pid, but can't restart again because his want the path of application but I don't want restart entire application I only want restart that specific process

How I can achieve this? Is possible to do this?

UPGRADE:

well, I think this understand better with an example, so here is:

First, imagine I want to watch the process with pid 12780 of the application Microsoft Edge.

enter image description here

Second, when this process exceeds the min consume of RAM or CPU what I set in my watchdog, that process should restart, begin with RAM and CPU in 0.

But here is the problem if I want to restart that process I can kill it, yes, but I can't start it after, Even if I set the full path of my application (in this case Microsoft Edge) it can't start again.

So, how I can restart only that process don't the full application?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • 5
    So your question is "how can I get the path of an application by its PID"? – mason Dec 27 '18 at 15:53
  • So all you want is to open a new tab in Edge? Note that the tab pool is quite important for Edge and killing it might close all tabs of Edge. – Emond Dec 27 '18 at 22:55

2 Answers2

7

Once you use How to get the full path of running process? to get the executable that was used to start the process you might be able to restart it.

However, if the executable was started in a specific way (think of a working folder, start parameters, environment variables, ...) it might not run the same way it was previously started. These special setting can be retrieved by using this answer: https://stackoverflow.com/a/5497319/563088 (commandline result)

The environment variables can be retrieved by using How to get the Process Environment Block (PEB) from extern process?

Emond
  • 50,210
  • 11
  • 84
  • 115
0

No, there is no way to do what you want.

If you wanted to restart a full application, that would be possible, but as you would potentially lose some data.

However, you say that you want to restart only a part of your application, say one of the processes, and that is not possible, unless it is specifically supported by the application itself - which is not generally the case.

Let the explain why.

When an application decides to create a new process, that process gets a copy (simplifying, but still) of the memory of the parent process at that moment, and after that both processes start diverging from that state, so it doesn't exist anymore.

Is it possible to create applications that allow you to restart it's processes? Yes. But most applications don't allow you to do that, and it's not even always clear what would mean to restart a process.

theMage
  • 79
  • 5
  • You seem to be thinking of "process" as a stream of actions that an application might perform. But it seems clear from the context that "process" is referring to an instance of a running application. In which case, it's entirely possible, as Erno's answer demonstrates. – mason Dec 27 '18 at 21:28
  • Hey @mason, from the update on the original question, it is not the whole instance, but a single subprocess of the instance that the author wants to restart - to restart the whole instance of the application you can get the starting environment, as previously stated. But not a single pid from a group. – theMage Dec 27 '18 at 21:43
  • If you look at the screenshot shared, it clearly indicate just one of the pids of the Microsoft edge application instance. – theMage Dec 27 '18 at 21:45
  • ok understood now @theMage, but are there a way for clear memory and CPU usage without restart or kill it with c#? is possible?. – Carlos Daniel Reyes Dominguez Dec 27 '18 at 23:46