How to get the length of time a process runs

Source: Internet
Author: User
Tags current time filetime

This article briefly describes how to use the Getprocesstimes API function to get how long a process has been running. The time value returned by the Getprocesstimes function can easily be converted into readable and available information. Refer to the following code snippet:

HANDLE hProcess;
FILETIME ftCreation,
ftExit,
ftKernel,
ftUser;

GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser);

The running interface of this example program is shown in the following illustration:

Calculate when to run

The length of time that a process is running is the time elapsed between the time the process was created and the current time. This information is stored in the FILETIME structure. As long as the elapsed time is calculated, it is converted into an hour/minute/second form. Luckily, with the help of the COleDateTime class, the job is easy to accomplish.

COleDateTime timeNow = COleDateTime::GetCurrentTime(),
timeCreation = ftCreation;
COleDateTimeSpan timeDiff = timeNow - timeCreation;

Here you can use different methods in COleDateTimeSpan to get information about the lost hours/minutes.

Calculate Kernel and User time

Refer to the documentation, kernel and user time is the actual elapsed time. This value is represented in the FILETIME structure as a 100 nanosecond unit. There are two ways to convert it to available information:

Method One:

We can use some basic methods to convert it to a value in seconds, a nanosecond equivalent to one out of 10,000 seconds, because this time has been expressed in 100 nanoseconds, so use 10个百万 to divide it:

__int64 i64Kernel = *((__int64 *) &ftKernel);
DWORD dwKernel = (DWORD) (i64Kernel / 10000000U);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.