简体   繁体   中英

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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