Finding lowest Cycle delta of a Thread in an application

void QueryThreadsCycleData(DWORD processID, std::unordered_map<DWORD, ULONG64>& previousCycleTimes,
    DWORD& threadWithLowestDelta, ULONG64& lowestCycleDelta,
    std::unordered_map<DWORD, bool>& suspendedThreads, bool& isValorantDetected,
    std::unordered_map<DWORD, bool>& existingThreads, std::unordered_map<DWORD, ULONG64>& newThreads,
    ULONG64& highestTotalCycles, ULONG64& lowestCycleDeltaSaved, std::unordered_map<DWORD, ULONG64>& newThreadsSaved) {

    THREADENTRY32 te32;
    HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
    if (hThreadSnap == INVALID_HANDLE_VALUE) {
        std::cerr << "CreateToolhelp32Snapshot failed: " << GetLastError() << "n";
        return;
    }

    te32.dwSize = sizeof(THREADENTRY32);

    if (!Thread32First(hThreadSnap, &te32)) {
        std::cerr << "Thread32First failed: " << GetLastError() << "n";
        CloseHandle(hThreadSnap);
        return;
    }

    lowestCycleDelta = ULLONG_MAX;

    std::cout << "Thread CPU Cycles Data for Process ID: " << processID << std::endl;

    do {
        if (te32.th32OwnerProcessID == processID) {
            HANDLE hThread = OpenThread(THREAD_QUERY_INFORMATION | THREAD_SUSPEND_RESUME, FALSE, te32.th32ThreadID);
            if (hThread == NULL) {
                std::cerr << "OpenThread failed: " << GetLastError() << "n";
                continue;
            }

            ULONG64 currentCycleTime = 0;
            if (QueryThreadCycleTime(hThread, &currentCycleTime)) {
                ULONG64 cycleDelta = 0;
                if (previousCycleTimes.find(te32.th32ThreadID) != previousCycleTimes.end()) {
                    ULONG64 previousCycleTime = previousCycleTimes[te32.th32ThreadID];
                    cycleDelta = (currentCycleTime > previousCycleTime) ? (currentCycleTime - previousCycleTime) : 0;
                }

                previousCycleTimes[te32.th32ThreadID] = currentCycleTime;

                std::cout << "Thread ID: " << te32.th32ThreadID
                    << " - Total Cycles: " << currentCycleTime
                    << " - Cycle Delta: " << cycleDelta << " cycles" << std::endl;

                if (cycleDelta >= 15000 && cycleDelta <= 100000) {
                    if (cycleDelta < lowestCycleDelta) {
                        lowestCycleDelta = cycleDelta;
                        threadWithLowestDelta = te32.th32ThreadID;
                    }
                }

                if (currentCycleTime > highestTotalCycles) {
                    highestTotalCycles = currentCycleTime;
                }

                if (isValorantDetected) {
                    if (existingThreads.find(te32.th32ThreadID) == existingThreads.end()) {
                        newThreads[te32.th32ThreadID] = currentCycleTime;
                        std::cout << "New thread detected: Thread ID " << te32.th32ThreadID << std::endl;
                    }
                }
                else {
                    existingThreads[te32.th32ThreadID] = true;
                }
            }
            else {
                std::cerr << "QueryThreadCycleTime failed for thread ID: " << te32.th32ThreadID
                    << ", error: " << GetLastError() << std::endl;
            }

            CloseHandle(hThread);
        }
    } while (Thread32Next(hThreadSnap, &te32));

    CloseHandle(hThreadSnap);

    if (isValorantDetected && !newThreads.empty()) {
        DWORD threadToSuspend = 0;
        ULONG64 maxCycles = 0;
        for (const auto& entry : newThreads) {
            if (entry.second > maxCycles) {
                maxCycles = entry.second;
                threadToSuspend = entry.first;
            }
        }

        if (threadToSuspend != 0) {
            std::this_thread::sleep_for(std::chrono::seconds(1));

            HANDLE hThreadSuspend = OpenThread(THREAD_SUSPEND_RESUME, FALSE, threadToSuspend);
            if (hThreadSuspend) {
                SuspendThread(hThreadSuspend);
                CloseHandle(hThreadSuspend);
                std::cout << "Suspended new thread with highest total cycles: Thread ID " << threadToSuspend << std::endl;
                std::cout << "Debug: Thread with highest total cycles has been suspended." << std::endl;

                if (threadWithLowestDelta != 0) {
                    HANDLE hThreadLowestDelta = OpenThread(THREAD_SUSPEND_RESUME, FALSE, threadWithLowestDelta);
                    if (hThreadLowestDelta) {
                        SuspendThread(hThreadLowestDelta);
                        CloseHandle(hThreadLowestDelta);
                        std::cout << "Suspended thread with lowest cycle delta: Thread ID " << threadWithLowestDelta << std::endl;
                        std::cout << "Debug: Thread with lowest cycle delta has been suspended." << std::endl;
                    }
                }
            }
        }
    }

    if (threadWithLowestDelta != 0 && suspendedThreads.empty()) {
        for (const auto& thread : previousCycleTimes) {
            if (thread.first != threadWithLowestDelta) {
                HANDLE hThreadSuspend = OpenThread(THREAD_SUSPEND_RESUME, FALSE, thread.first);
                if (hThreadSuspend) {
                    SuspendThread(hThreadSuspend);
                    CloseHandle(hThreadSuspend);
                    suspendedThreads[thread.first] = true;
                }
            }
        }
    }

    if (threadWithLowestDelta != 0 && lowestCycleDelta != ULLONG_MAX) {
        std::cout << "Lowest Cycle Delta Thread ID: " << threadWithLowestDelta << std::endl;
        std::cout << "Cycle Delta: " << lowestCycleDelta << std::endl;
    }
    else {
        std::cout << "No thread with cycle delta within the specified range found." << std::endl;
    }

    std::cout << "Highest Total Cycles: " << highestTotalCycles << std::endl;

    // Save the results to a file
    std::ofstream outFile("thread_data.txt");
    if (outFile.is_open()) {
        outFile << "Lowest Cycle Delta: " << lowestCycleDelta << std::endl;
        outFile << "Highest Total Cycles: " << highestTotalCycles << std::endl;

        if (isValorantDetected && !newThreads.empty()) {
            outFile << "New Threads:" << std::endl;
            for (const auto& entry : newThreads) {
                outFile << "Thread ID: " << entry.first << " - Total Cycles: " << entry.second << std::endl;
            }
        }
        else {
            outFile << "No new threads detected." << std::endl;
        }
        outFile.close();
    }
    else {
        std::cerr << "Failed to open file for writing." << std::endl;
    }
}

this is my function, but somehow it’s unable to find the total lowest cycles delta and displays as 0:

As you can see in the output, every lowest cycle delta is displayed as 0. I want to find the lowest cycle delta for the thread, then suspend every thread except the lowest cycle delta. however, currently, as seen in the code every cycle delta is now 0, after the while loop was removed.

does anyone know how to resolve this issue without having to run a loop!?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật