locked
Loop Func. Freezes the GUI RRS feed

  • Question

  • Hello. I'm trying to loop a function but it freezes my GUI when i activate it and i can't do anything anymore on GUI when this happens. Here is the part of it;

    DWORD Conqq = 0x0053A124;
    
    void ConqqFunction(bool fEnable)
    {
     if(fEnable)
     {
         while(1) {
         for( ; ; )
       {
    WriteMemory(Conqq, 1, 0xD2);
    Sleep(200);
    WriteMemory(Conqq, 1, 0xD9);
    Sleep(6000);
         }
    }
    }
    }


    And the checkbox thingy;

    void Form1::checkBox3_CheckedChanged(System::Object^  sender, System::EventArgs^  e)
    {
             ConqqFunction(this->checkBox3->Checked);
    }

    Sorry, it's poorly coded. Anyway,

    How can i fix that? Would multithreading work? Thanks in advance.

    Monday, January 12, 2015 12:48 PM

Answers

  • >How can i fix that? Would multithreading work?

    Yes, you need to put your time consuming code into its own worker
    thread in order to leave the GUI thread free to service the UI.

    Dave

    • Proposed as answer by Bordon Monday, January 12, 2015 2:20 PM
    • Marked as answer by Shu 2017 Tuesday, January 20, 2015 1:43 AM
    Monday, January 12, 2015 1:39 PM
  • Hello. I'm trying to loop a function but it freezes my GUI when i activate it and i can't do anything anymore on GUI when this happens. Here is the part of it;

    DWORD Conqq = 0x0053A124;
    
    void ConqqFunction(bool fEnable)
    {
        if(fEnable)
        {
            while(1) 
            {
                for( ; ; )
                {
                    WriteMemory(Conqq, 1, 0xD2);
                    Sleep(200);
                    WriteMemory(Conqq, 1, 0xD9);
                    Sleep(6000);
                }
            }
        }
    }
    

    And the checkbox thingy;

    void Form1::checkBox3_CheckedChanged(System::Object^  sender, System::EventArgs^  e)
    {
        ConqqFunction(this->checkBox3->Checked);
    }
    

    Sorry, it's poorly coded. Anyway,

    How can i fix that? Would multithreading work? Thanks in advance.

    As David Lowndes says, you need to put time-consuming code into a worker thread.

    But your particular function seems very strange.

    1. Why do you have both while(1) and for(;;)?

    2. Your function has no provision for exiting. Even if the GUI were not frozen and you could un-select the check-box, your code would launch a new ConqqFunction. The original one would keep on running.


    David Wilkinson | Visual C++ MVP

    • Marked as answer by Shu 2017 Tuesday, January 20, 2015 1:43 AM
    Monday, January 12, 2015 2:43 PM

All replies

  • >How can i fix that? Would multithreading work?

    Yes, you need to put your time consuming code into its own worker
    thread in order to leave the GUI thread free to service the UI.

    Dave

    • Proposed as answer by Bordon Monday, January 12, 2015 2:20 PM
    • Marked as answer by Shu 2017 Tuesday, January 20, 2015 1:43 AM
    Monday, January 12, 2015 1:39 PM
  • Hello. I'm trying to loop a function but it freezes my GUI when i activate it and i can't do anything anymore on GUI when this happens. Here is the part of it;

    DWORD Conqq = 0x0053A124;
    
    void ConqqFunction(bool fEnable)
    {
        if(fEnable)
        {
            while(1) 
            {
                for( ; ; )
                {
                    WriteMemory(Conqq, 1, 0xD2);
                    Sleep(200);
                    WriteMemory(Conqq, 1, 0xD9);
                    Sleep(6000);
                }
            }
        }
    }
    

    And the checkbox thingy;

    void Form1::checkBox3_CheckedChanged(System::Object^  sender, System::EventArgs^  e)
    {
        ConqqFunction(this->checkBox3->Checked);
    }
    

    Sorry, it's poorly coded. Anyway,

    How can i fix that? Would multithreading work? Thanks in advance.

    As David Lowndes says, you need to put time-consuming code into a worker thread.

    But your particular function seems very strange.

    1. Why do you have both while(1) and for(;;)?

    2. Your function has no provision for exiting. Even if the GUI were not frozen and you could un-select the check-box, your code would launch a new ConqqFunction. The original one would keep on running.


    David Wilkinson | Visual C++ MVP

    • Marked as answer by Shu 2017 Tuesday, January 20, 2015 1:43 AM
    Monday, January 12, 2015 2:43 PM
  • On 12/01/2015 13:48, hatsunemiku94 wrote:

    Hello. I'm trying to loop a function but it freezes my GUI when i activate it and i can't do anything anymore on GUI when this happens.

    In general, when you have a lengthy task, it's a good idea to assign it to a worker thread, keeping the GUI active while the task is being performed.

    You might find this essay by Joe Newcomer interesting:

    "Using Worker Threads"
    http://www.flounder.com/workerthreads.htm

    Giovanni

    Monday, January 12, 2015 3:03 PM
  • Apart from above mentioned suggestion to me it looks like you are running in a infinite loop . Check your loop again does it ever get break.This loop will for sure going to freeze your GUI

    Thanks


    Rupesh Shukla

    Monday, January 12, 2015 3:06 PM