簡體   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 

是的,使用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