0

Is there a way to know the window names and duration of the current windows open in c# and get a callback when the windows have closed?

Jaseem Abbas
  • 5,028
  • 6
  • 48
  • 73
  • 2
    Possible duplicate of [How can I list all processes running in Windows?](http://stackoverflow.com/questions/648410/how-can-i-list-all-processes-running-in-windows) – Adrian Fâciu Jun 22 '16 at 14:23

2 Answers2

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 
0

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
}
endofzero
  • 1,830
  • 1
  • 22
  • 33