【问题标题】:How to get duration with the current active processes in c#?如何在 c# 中获取当前活动进程的持续时间?
【发布时间】:2016-06-22 14:17:02
【问题描述】:

有没有办法知道在 c# 中打开的当前窗口的窗口名称和持续时间,并在窗口关闭时获得回调?

【问题讨论】:

标签: c#


【解决方案1】:
using System.Diagnostics;

Process[] processlist = Process.GetProcesses();

foreach (Process process in processlist)
{
    if (!String.IsNullOrEmpty(process.MainWindowTitle))
        Console.WriteLine("Process: {0} ID: {1} Window title: {2}" duration: {3}" , process.ProcessName, process.Id, process.MainWindowTitle, process.duration);
}

// i'm not sure if process.duration actually exists but it would be something like that 

【讨论】:

    【解决方案2】:

    是的,使用 Process 类您可以获得该信息。

    using System.Diagnostics;
    
    public void GetProcessesInfo()
    {
        Process[] allProcesses = Process.GetProcesses();
    
        foreach (Process process in allProcesses)
        {
            try
            {
                string windowName = process.MainWindowTitle;
                TimeSpan duration = DateTime.Now - process.StartTime;
                process.EnableRaisingEvents = true;
                process.Exited += new EventHandler(process_Exited);
            }
            catch(System.ComponentModel.Win32Exception)
            {
                //access to that process was denied
            }
        }
    }
    
    void process_Exited(object sender, EventArgs e)
    {
        //a process has exited
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-17
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 2019-10-26
      • 1970-01-01
      相关资源
      最近更新 更多