How to get the time needed to run the application?

I am writing a C # application that should be able to tell how long it takes to open a specific application. I use the stopwatch class as my timer. Starting time is easy, since I installed it with the call to run .exe. The problem is how to determine when the program opens. The only thing I could think of to check this out was to use the PerformanceCounter object and check when the% processor is less than a certain number using the while loop.

I'm currently using PerformanceCounter, but I’m unlucky in that it shows CPU% (it always displays 0). My assumption is that the application opens faster than the PerformanceCounter can check for the% processor, or that the PerformanceCounter does not see the name of the process because I call it too quickly (I very much doubt the latter because I think I I will get errors if this happens).

Are there any other ways to solve this problem? Is there something I can do wrong that gives me 0% CPU all the time? I am not looking for external tools outside my application. Here is an example of my code:

otherApp = new otherApplication.Application();
PerformanceCounter cpuCounter = new PerformanceCounter("Process",
"% Processor Time", "otherApplication");
//This next line is to check if PerformanceCounter object is working
//MessageBox.Show(cpuCounter.NextValue().ToString());
stopwatch.Start();
while (cpuCounter.NextValue() > 10)
{
}
stopwatch.Stop();

Edited: Changed code to say otherApp and otherApplication instead of myApp and myApplication to make it easier to understand.

+5
4

( , , ), .

+2

, , , Process.WaitForInputIdle.

, . , , 1.

, WaitForProcessStartupComplete.

using System;
using System.Diagnostics;

class StartupWatch
{
    static void Main()
    {
        string application = "calc.exe";
        Stopwatch sw = Stopwatch.StartNew();
        Process process = Process.Start(application);
        process.WaitForInputIdle();
        Console.WriteLine("Time to start {0}: {1}", application, sw.Elapsed);
    }
}

1 , , . , , .

Update:

COM-, Process.Start, AccessibleWindowFromObject COM- . , .

, Word Word.Application, . , COM-.

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Word = Microsoft.Office.Interop.Word;

[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00020400-0000-0000-C000-000000000046")]
public interface IDispatch
{
}

class StartupWatch
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("Oleacc.dll")]
    static extern int AccessibleObjectFromWindow(IntPtr hwnd, uint dwObjectID, byte[] riid, out IDispatch ptr);

    public delegate bool EnumChildCallback(IntPtr hwnd, ref IntPtr lParam);

    [DllImport("User32.dll")]
    public static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildCallback lpEnumFunc, ref IntPtr lParam);

    [DllImport("User32.dll")]
    public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

    public static bool EnumChildProc(IntPtr hwndChild, ref IntPtr lParam)
    {
        StringBuilder buf = new StringBuilder(128);
        GetClassName(hwndChild, buf, 128);
        if (buf.ToString() == "_WwG")
        {
            lParam = hwndChild;
            return false;
        }
        return true;
    }

    static Word.Application GetWordApplicationObject(Process process)
    {
        Word.Application wordApp = null;
        if (process.MainWindowHandle != IntPtr.Zero)
        {
            IntPtr hwndChild = IntPtr.Zero;

            // Search the accessible child window (it has class name "_WwG") 
            // as described in http://msdn.microsoft.com/en-us/library/dd317978%28VS.85%29.aspx
            //
            // adjust this class name inside EnumChildProc accordingly if you are 
            // creating another COM server than Word
            //
            EnumChildCallback cb = new EnumChildCallback(EnumChildProc);
            EnumChildWindows(process.MainWindowHandle, cb, ref hwndChild);

            if (hwndChild != IntPtr.Zero)
            {
                // We call AccessibleObjectFromWindow, passing the constant OBJID_NATIVEOM (defined in winuser.h) 
                // and IID_IDispatch - we want an IDispatch pointer into the native object model.
                //
                const uint OBJID_NATIVEOM = 0xFFFFFFF0;
                Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}");
                IDispatch ptr;

                int hr = AccessibleObjectFromWindow(hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), out ptr);
                if (hr >= 0)
                {
                    // possibly adjust the name of the property containing the COM  
                    // object accordingly
                    // 
                    wordApp = (Word.Application)ptr.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, ptr, null);
                }
            }
        }
        return wordApp;
    }

    static void Main(string[] args)
    {
        Stopwatch sw = Stopwatch.StartNew();
        Process process = Process.Start(@"C:\Program Files (x86)\Microsoft Office\Office12\WINWORD.EXE");
        process.WaitForInputIdle();
        Console.WriteLine("Time to start {0}: {1}", "Word", sw.Elapsed);
        Word.Application wordApp = GetWordApplicationObject(process);
        Console.WriteLine(string.Format("Word version is: {0}", wordApp.Version));
    }
}
+3

, System.Threading.Mutex myApplication otherApplication. , otherApplication , .

+2

, .

. , . , , . , JIT . , .NET . , , .

. , . , Application.Exit() Shown , , . , . , .bat, :

time /t
start /wait yourapp.exe
time /t

, , , . , , , . . .

+1

All Articles