1

I have to track the running time of a program. That program exposes the following window enter image description here

At the same time I launch my program which in a timer does:

private void TimerCheckGroups_Tick(object sender, EventArgs e)
{
  IntPtr windowPtr = FindWindowByCaption(IntPtr.Zero, "Execution");
  if (windowPtr != IntPtr.Zero)
    Console.Beep();<--------
}

But the beep line never gets hit. Have I misunderstood the meaning of a window caption?

--ADD-- I'll try to make the execution phases clearer.

Startup----> launch my logger.

User-------> launches program A that launches program B (not visible) that launches window C. C has caption Execution.

When I launch the solution proposed by dontbyteme the only the B program appears so only 1 window.

In short

  • logger: not visible since it's a tray program

  • program A: visible since it's the main program

  • program B: not visible since it's set to Notvisible

  • program C: not visible why?!?!?!?


--SOLUTION THANX TO JARRETT--

  • logger stays idle with a timer monitoring processes

  • program A starts but nobody cares about it. Then program A launches program B

  • when program B is awake i find the window and start logging

Patrick
  • 3,073
  • 2
  • 22
  • 60
  • 1
    Can you make your program launch the Execution program? If you have to start both programs manually your time will not be correct. Also, by having your tracking program start the execution program you know exactly when it started. – Jarrett Robertson Dec 22 '15 at 13:46
  • No I can't. The program with the window execution is launched by the user. While my program is a logger so it's launched on window startup. – Patrick Dec 22 '15 at 13:56
  • Depending on how comfortable you are with marshalling have you looked into `EnumWindows I have used it to find windows that are open. It is in user32.dll. This will give you the windows that are open but wouldn't address knowing that a new program was launched. – Jarrett Robertson Dec 22 '15 at 13:59
  • Also, this post might be useful to you. http://stackoverflow.com/questions/649900/detecting-the-launch-of-a-application – Jarrett Robertson Dec 22 '15 at 14:01
  • @JarrettRobertson thanx is it for WPF? – Patrick Dec 22 '15 at 14:18
  • No, but I imagine you could get it to work with WPF easily enough. I'll admit my knowledge of WPF is limited but I know you can write c# code in it. – Jarrett Robertson Dec 22 '15 at 14:22
  • @JarrettRobertson Nothing WPF-specific in the interop code you posted in your answer, nor in the linked SO question/answer. Original poster should be good-to-go. – Steven Rands Dec 22 '15 at 15:41

2 Answers2

1

The following question addresses how to find out when programs launch. Detecting the launch of a application Also, you can enumerate windows on your machine with a dll import and using EnumWindows. Sample pInvokes that will help you are listed.

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int GetWindowTextLength(IntPtr hWnd);
    [DllImport("user32.dll")]
    private static extern bool IsWindowVisible(IntPtr hWnd);
Community
  • 1
  • 1
  • So the problem was related with timing please see my edit since I have followed your hint to get to the solution. I wish you posted a solution saying that the problem is related with launch simultaneously both program. I think you deserve it. – Patrick Dec 22 '15 at 15:07
0

You can try getting the window by running through each window and compare to the title:

    foreach(Window window in Application.Current.Windows)
    {
        if(window.Title == "Execution")
        {
            Console.Beep();
            // ...
        }
    }

The Title property is what you called Caption.

dontbyteme
  • 1,221
  • 1
  • 11
  • 23