Home > Backend Development > C++ > How Can I Accurately Track Process Execution Time in .NET?

How Can I Accurately Track Process Execution Time in .NET?

Barbara Streisand
Release: 2025-01-21 00:42:09
Original
254 people have browsed it

How Can I Accurately Track Process Execution Time in .NET?

Tracking Process Runtime with Enhanced .NET Techniques

Simply using Process.GetProcessesByName offers only a momentary view of active processes; it's insufficient for tracking a process's execution duration across multiple instances. A more robust solution is needed.

Leveraging WMI for Accurate Process Monitoring

Windows Management Instrumentation (WMI) provides a powerful mechanism for detailed system event monitoring, including the precise start and end times of processes. The following C# code demonstrates WMI's application for this purpose:

<code class="language-csharp">using System;
using System.Management;

public class ProcessMonitor
{
    public static void Main(string[] args)
    {
        // Watch for process start events
        using (ManagementEventWatcher startWatch = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace")))
        {
            startWatch.EventArrived += StartWatch_EventArrived;
            startWatch.Start();

            // Watch for process stop events
            using (ManagementEventWatcher stopWatch = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace")))
            {
                stopWatch.EventArrived += StopWatch_EventArrived;
                stopWatch.Start();

                Console.WriteLine("Monitoring process starts and stops. Press any key to exit.");
                Console.ReadKey();
            }
        }
    }

    private static void StopWatch_EventArrived(object sender, EventArrivedEventArgs e)
    {
        Console.WriteLine($"Process stopped: {e.NewEvent.Properties["ProcessName"].Value}");
    }

    private static void StartWatch_EventArrived(object sender, EventArrivedEventArgs e)
    {
        Console.WriteLine($"Process started: {e.NewEvent.Properties["ProcessName"].Value}");
    }
}</code>
Copy after login

Executing this code with administrator privileges allows real-time monitoring of process lifecycle events. This provides a highly accurate measurement of execution time for any process, across multiple executions. Keep in mind that intensive monitoring might impact system performance.

The above is the detailed content of How Can I Accurately Track Process Execution Time in .NET?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template