C#一个进程监控另一进程的用户界面是否响应系统

时间:2022-08-29 14:45:08

Process.Responding Property

定义

获取指示进程的用户界面当前是否响应的值。

获取指示进程的用户界面当前是否响应的值。

C#
public bool Responding { get; }

属性值

如果关联进程的用户界面当前响应系统,则为 true;否则为 false

异常

该平台是 Windows 98 或 Windows Millennium Edition (Windows Me);将 UseShellExecute 设置为 false,以在 Windows 98 和 Windows Me *问此属性。

没有与此 Process 对象关联的进程。

你正试图访问在远程计算机上运行的进程的 Responding 属性。 此属性仅可用于本地计算机上运行的进程。

示例

以下示例启动记事本的实例。 该示例然后检索并显示关联的进程的各种属性。 该示例检测时在进程退出,并显示该进程的退出代码。

using System;
using System.Diagnostics;

namespace ProcessSample
{
    class ProcessMonitorSample
    {
        public static void Main()
        {
            // Define variables to track the peak
            // memory usage of the process.
            long peakPagedMem   = 0,
                 peakWorkingSet = 0,
                 peakVirtualMem = 0;

            // Start the process.
            using (Process myProcess = Process.Start("NotePad.exe"))
            {
                // Display the process statistics until
                // the user closes the program.
                do
                {
                    if (!myProcess.HasExited)
                    {
                        // Refresh the current process property values.
                        myProcess.Refresh();

                        Console.WriteLine();

                        // Display current process statistics.

                        Console.WriteLine($"{myProcess} -");
                        Console.WriteLine("-------------------------------------");

                        Console.WriteLine($"  Physical memory usage     : {myProcess.WorkingSet64}");
                        Console.WriteLine($"  Base priority             : {myProcess.BasePriority}");
                        Console.WriteLine($"  Priority class            : {myProcess.PriorityClass}");
                        Console.WriteLine($"  User processor time       : {myProcess.UserProcessorTime}");
                        Console.WriteLine($"  Privileged processor time : {myProcess.PrivilegedProcessorTime}");
                        Console.WriteLine($"  Total processor time      : {myProcess.TotalProcessorTime}");
                        Console.WriteLine($"  Paged system memory size  : {myProcess.PagedSystemMemorySize64}");
                        Console.WriteLine($"  Paged memory size         : {myProcess.PagedMemorySize64}");

                        // Update the values for the overall peak memory statistics.
                        peakPagedMem   = myProcess.PeakPagedMemorySize64;
                        peakVirtualMem = myProcess.PeakVirtualMemorySize64;
                        peakWorkingSet = myProcess.PeakWorkingSet64;

                        if (myProcess.Responding)
                        {
                            Console.WriteLine("Status = Running");
                        }
                        else
                        {
                            Console.WriteLine("Status = Not Responding");
                        }
                    }
                }
                while (!myProcess.WaitForExit(1000));


                Console.WriteLine();
                Console.WriteLine($"  Process exit code          : {myProcess.ExitCode}");

                // Display peak memory statistics for the process.
                Console.WriteLine($"  Peak physical memory usage : {peakWorkingSet}");
                Console.WriteLine($"  Peak paged memory usage    : {peakPagedMem}");
                Console.WriteLine($"  Peak virtual memory usage  : {peakVirtualMem}");
            }
        }
    }
}

 

注解

如果某个进程拥有的用户界面,Responding属性联系来确定进程是否正在响应用户输入的用户界面。如果接口不会立即响应Responding属性返回false。 使用此属性确定关联的进程的界面已停止响应。

如果进程不具有MainWindowHandle,此属性返回true

应用示例

C#一个进程监控另一进程的用户界面是否响应系统C#一个进程监控另一进程的用户界面是否响应系统
 1  public static void Main(string[] args)
 2         {
 3             Process[] process = Process.GetProcessesByName("应用程序进程名");
 4            
 5             if (process.Length > 0)
 6             {
 7                 Process myProcess = process[0];
 8 
 9                 do
10                 {
11                     if (!myProcess.HasExited)
12                     {
13                         myProcess.Refresh();
14                         if (myProcess.Responding)
15                         {
16                             //Console.WriteLine("Status = Running");
17                         }
18                         else
19                         {
20                             //Console.WriteLine("Status = Not Responding");
21                             //无响应处理过程。。。。
22                            
23                         }
24                     }
25                 }
26                 while (!myProcess.WaitForExit(ReadWatchTime()));
27             }
28         }
View Code