我正在嘗試開發一款應用程序,該應用程序將監視位於我的PC上的服務器上的進程的內存使用情況。我已經創建了一個應用程序,該應用程序可以監視與應用程序運行在同一臺PC上的進程的內存使用情況,但無法弄清楚如何從我的PC運行應用程序並監視另一臺PC /服務器的進程。監視另一臺服務器上的進程的內存使用情況
任何幫助都會指引我走向正確的道路。
UPDATE
這是我到目前爲止有:
private static string sProcName = "PCMain";
private static string machine = "serverName";
private static int sProcMemoryInKB = 10000;
static void Main(string[] args) {
VerifyRemoteMachineStatus(machine);
GetMachineName();
Process ReqProcess;
GC.GetTotalMemory(true);
ReqProcess = CurrentlyRunning(sProcName);
do {
if (ReqProcess != null) {
System.Threading.Thread.Sleep(1000);
// calculate the CPU load
System.TimeSpan CPULoad = (DateTime.Now - ReqProcess.StartTime);
Console.WriteLine("CPU Load: " + (ReqProcess.TotalProcessorTime.TotalMilliseconds/CPULoad.TotalMilliseconds) * 100);
PerformanceCounter WorkingSetMemoryCounter = new PerformanceCounter("Process", "Working Set", sProcName, machine);
//System.Threading.Thread.Sleep(1000);
Console.WriteLine("Momory (Working Set) " + (WorkingSetMemoryCounter.NextValue()/1024) + "K");
PerformanceCounter WorkingSetPrivateMemoryCounter = new PerformanceCounter("Process", "Working Set - Private", ReqProcess.ProcessName, machine);
//System.Threading.Thread.Sleep(1000);
Console.WriteLine("Momory (Private Working Set) " + (WorkingSetPrivateMemoryCounter.NextValue()/1024) + "K" + Environment.NewLine + Environment.NewLine);
//if ((WorkingSetMemoryCounter.NextValue()/1024) >= sProcMemoryInKB) {
// SendMail();
//}
}
} while (true);
}
private static Process CurrentlyRunning(string sProcessName) {
//get a list of all running processes on current system
Process[] Processes = Process.GetProcesses();
//Iterate to every process to check if it is out required process
foreach (Process SingleProcess in Processes) {
if (SingleProcess.ProcessName.Contains(sProcessName)) {
//process found
return SingleProcess;
}
}
//Process not found
return null;
}
private static void GetMachineName() {
string[] cmdArgs = System.Environment.GetCommandLineArgs();
if ((cmdArgs != null) && (cmdArgs.Length > 1)) { machine = cmdArgs[1]; }
}
// ping the remote computer
private static bool VerifyRemoteMachineStatus(string machineName) {
try {
using (Ping ping = new Ping()) {
PingReply reply = ping.Send(machineName);
if (reply.Status == IPStatus.Success) { return true; }
}
} catch (Exception ex) {
// return false for any exception encountered
// we'll probably want to just shut down anyway
}
return false;
}
可能的重複 http://stackoverflow.com/questions/865412/check-if-a-process-is-running-on-a-remote-system-using-c-sharp 檢查鏈接,它可能會有所幫助! –