Question

I came up with the idea of putting the for loop in a thread, so that all I would have to do is pause or delete the thread to break the for loop. However, it gets a little more complicated than that. I am making a Call of Duty Modern Warfare 2 mod menu, which is coded in c++ and built as a .dll file. I have made it so that a function I called disco fog is an option in that menu. What I want to do is make disco fog toggleable. So here is what I have tried so far. (Keep in mind that in each idea, the thread was just re-created, not ever broken).

DWORD WINAPI DFOG(LPVOID)
{
    if(GetBool("cl_ingame"))
    {
        for(;;)
        {
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 0 0 0.6 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 0.2 0.7 1 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 0.9 0 1 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 0.9 0.4 1 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 1 0 0 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 0 1 0 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 1 1 0 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 0 0 0 1 0 0");
            Sleep(250);
        }
    }
    return 0;
}

void DiscoFog(int Client, int Input)
{
    HANDLE DF = CreateThread(0,0,DFOG,0,0,0);
    if(DiscoFogToggle == false)
    {
        DiscoFogToggle = true;
        ResumeThread(DF);
    }
    else
    {
        DiscoFogToggle = false;
        SuspendThread(DF);
        SV_ClientSendServerCommand(Client, 0, "d 13 0 1 0 0 0 0 0 0");
    }
}

As you can see I have tried pausing and resuming the thread, however it only recreates the thread for some reason. The following is what I tried when deleting the thread and recreating it to toggle disco fog:

DWORD WINAPI DFOG(LPVOID)
{
    if(GetBool("cl_ingame"))
    {
        for(;;)
        {
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 0 0 0.6 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 0.2 0.7 1 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 0.9 0 1 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 0.9 0.4 1 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 1 0 0 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 0 1 0 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 1 1 0 1 0 0");
            Sleep(250);
            SV_ClientSendServerCommand(-1, 0, "d 13 256 0.00135 0 0 0 1 0 0");
            Sleep(250);
        }
    }
    return 0;
}

void DiscoFog(int Client, int Input)
{
    HANDLE DF = CreateThread(0,0,DFOG,0,0,0);
    if(DiscoFogToggle == false)
    {
        DiscoFogToggle = true;
        DF;
    }
    else
    {
        DiscoFogToggle = false;
        CloseHandle(DF);
        SV_ClientSendServerCommand(Client, 0, "d 13 0 1 0 0 0 0 0 0");
    }
}

With c++, the computer is ALWAYS right, or in this case, the xbox is ALWAYS right. What I don't understand is what I can do so that my disco fog will work? Any help is appreciated :)

Was it helpful?

Solution

The thread is like the main() function in C++. In order to exit, it needs to return.

So what you need to do is signal to the thread that it needs to exit.

This is often done by some sort of variable.

e.g.

volatile int global_run = true;


 if(GetBool("cl_ingame"))
 {
   while(global_run) 
   {
      // ...
   }
 }

 // So when global_run == false it'll break out of the loop and the thread will exit.


And then in your main thread,
global_run = false;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top