简体   繁体   English

如何使用C#中的当前活动进程获取持续时间?

[英]How to get duration with the current active processes in c#?

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

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 

Yes, using the Process class you can get that information. 是的,使用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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM