0

i have a Qdialogbox and in this qdialogbox i have a thread(named thread3) which executes Print_Descendants_key(IUIAutomation* pUIAutomation, IUIAutomationElement* pParent, int indent) function, in this thread3.

so in my Accepted event(when i click okay in buttonbox) and closeEvent of dialog box, i want to quit/terminate this thread3. How can i do that ?

probably something like this ??

void KeyComd::closeEvent(QCloseEvent* event)
{
    std::terminate();
    thread3.terminate(); ??
}

void KeyComd::accepted()
{
    std::terminate();
}

for reference here is my QDialog code

#include "KeyComd.h"
#include "ui_KeyComd.h"
#include <QtCore>
#include <QtGui>
#include <vector> 
#include<QDebug>
#include "ExecutionContext.h"
#include "XMLParser.h"
#include "Logger.h"
#include "BlockCommand.h"
#include "UIAElementUtils.h"

ExecutionContext exc;
QStringList refreshed_elements;

KeyComd::KeyComd(QWidget *parent)
    : QDialog(parent)
{
    ui.setupUi(this);
    HRESULT hr = exc.init();    
}

KeyComd::~KeyComd()
{
}
void KeyComd::on_showbutton_clicked()
{
    ui.elements_listwidget->clear();
    desktop_elements.clear();

    std::thread thread3(&KeyComd::Print_step, this); // Here it calls a thread, because of this thread ,the execution of "Print_Descendants_key" function happens in a separate thread from main thread
    thread3.detach();
}

void KeyComd::Print_step()
{
    Print_Descendants_key(exc.pUIAutomation, nullptr, 0);
}

void KeyComd::Print_Descendants_key(IUIAutomation* pUIAutomation, IUIAutomationElement* pParent, int indent)
{
    ///Function which appends 1000 list-items in a QListWidget called "elements_listwidget" in my QDialog.
}
the_learnist
  • 69
  • 1
  • 8
  • Just to clarify- are you really asking how to terminate a thread or are you asking how to access the accept and close events in general as well? Assumed the former in my answer as contents of the question seem to heavily lean towards just terminating the thread but may have misunderstood – DragonJawad Aug 06 '20 at 05:25

1 Answers1

0

There's no good way to force terminate a single thread- see this related SO answer. However, what you can do is signal that thread to end itself. Here's another relevant SO question, but also wrote a quick example demonstrating the technique with an atomic_bool:

#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>

std::atomic_bool terminateThreads; // Atomic for setting and reading safely across threads

void runInfinitely() {
    while (!terminateThreads) {
        // Do some work
        std::cout << "Running infinite thread..." << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(150));
    }
    std::cout << "Terminated thread" << std::endl;
}

int main() {
    // Create your thread as you normally would
    std::thread infThread(runInfinitely);

    // Whatever else you like to do...
    std::this_thread::sleep_for(std::chrono::seconds(1));

    // Once you're ready to end it, just set the atomic flag as appropriate
    terminateThreads = true;

    infThread.join(); // Optional in context of this SO question
    std::cout << "End of main" << std::endl;
    return 0;
}

However, going by that comment in KeyComd::Print_Descendants_key it seems like the function has a finite length and thus will naturally finish + clean itself up (relevant SO answer here). Thus, if you're fine waiting until the thread's function naturally ends, then you're not required to "terminate" or clean up the thread manually at all.

DragonJawad
  • 1,846
  • 3
  • 20
  • 28