首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我需要帮助,如何使循环在一个情况下,它是在一个并发循环。

我需要帮助,如何使循环在一个情况下,它是在一个并发循环。
EN

Stack Overflow用户
提问于 2021-12-22 18:38:52
回答 1查看 46关注 0票数 0

我试图在一个带do-while循环的情况下做一个while (true)循环,但是当我将while (true)放在这个例子中时,菜单不会返回到控制台,我需要关闭调试器并再次运行它--有人可以帮助我,帮助我熟悉c++。

这是我的代码:

代码语言:javascript
复制
do
{
    std::cout << "[0] Quit\n"; // This is Option 0 of the Menu
    std::cout << "[1] Infinite Health\n"; // This is Option 1 of the Menu
    std::cout << "[2] Infinite Ammo\n"; // This is Option 2 of the Menu
    std::cout << "[3] Infinite Rounds\n"; // This is Option 3 of the Menu
    std::cin >> choice;


    switch (choice) // This is to detect the Choice the User selected
    {
    case 0:
        std::cout << "Why did you even open me to not use me :(\n";
        return 0;

    case 1:
        std::cout << "You Have Activated Infinite Health!\n";
        
        while (true)
        {
            int health = 1000;
            WriteProcessMemory(phandle, (LPVOID*)(healthPtrAddr), &health, 4, 0);
        }
        break;
    case 2:
        std::cout << "You Have Activated Infinite Ammo On Primary Weapon!\n";

        while (true)
        {
            int ammo = 500;
            WriteProcessMemory(phandle, (LPVOID*)(ammoPtrAddr), &ammo, 4, 0);
        }
        break;
    case 3:
        std::cout << "You Have Activated Infinite Rounds On Primary Weapon!";

        while (true)
        {
            int rounds = 200;
            WriteProcessMemory(phandle, (LPVOID*)(roundsPtrAddr), &rounds, 4, 0);
        }
        break;
    }
} 
while (choice !=0);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-22 18:51:43

是的,它不会返回,因为它阻塞了程序。

为了解决您的问题,可以将循环放入另一个线程中。

如果使用以下方法包括线程库:

代码语言:javascript
复制
#include <thread>

然后,您必须定义应该运行的函数:

代码语言:javascript
复制
void keepHealth() {
    while (true)
    {
        int health = 1000;
        WriteProcessMemory(phandle, (LPVOID*)(healthPtrAddr), &health, 4, 0);
    }
}

现在,您可以在另一个线程中执行此函数:

代码语言:javascript
复制
std::thread task1(keepHealth);

如果您想传递像句柄这样的参数,就必须在函数头中写入它们:

代码语言:javascript
复制
void keepHealth(void* pHandle, void* healthPtrAddress) {
    while (true)
    {
        int health = 1000;
        WriteProcessMemory(phandle, (LPVOID*)(healthPtrAddr), &health, 4, 0);
    }
}

然后像这样递过来:

代码语言:javascript
复制
std::thread task1(keepHealth, pHandle, healthPtrAddress);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70453709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档