Thread completion event not being picked up in handler Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
sw
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sat Mar 16, 2019 8:09 pm

Thread completion event not being picked up in handler

Post by sw »

Hi all,

I am running a thread from a dialog which is making a GET request to check for updates. So I followed the guide here https://docs.wxwidgets.org/trunk/classwx_thread.html. The only difference from there is that the handler had to be changed from EVT_COMMAND to EVT_THREAD. I get a compilation error in the event handler if I try to use EVT_COMMAND which is what is in the documentation. I found on another post here to EVT_THREAD and it compiled fine...

My problem is that I can see the thread runs and when it completes it sends a event to the handler via wxQueueEvent, but the dialog handler function is not being executed when the event gets sent. I have verified that (through wxLogDebug statements) it does queue the event and exits the Entry function of the thread.

I have also defined my custom event to handle the thread completion event. I have done custom events before, but I haven't done this with threads yet so I wonder what I'm doing something wrong...

Code: Select all

wxDEFINE_EVENT(CHECK_UPDATE_THREAD_COMPLETED, wxThreadEvent);

wxThread::ExitCode CheckForUpdateThread::Entry()
{
    std::string eventString = "";

    while (!TestDestroy()) {
        // check for update (omitted for brevity)
    }

    auto event = new wxThreadEvent(CHECK_UPDATE_THREAD_COMPLETED);
    event->SetString(eventString);
    wxQueueEvent(pHandler, event);

    return (wxThread::ExitCode) 0;
}

// CheckForUpdateDialog
wxBEGIN_EVENT_TABLE(CheckForUpdateDialog, wxDialog)
// ...
EVT_THREAD(CHECK_UPDATE_THREAD_COMPLETED, CheckForUpdateDialog::OnThreadCompletion)
wxEND_EVENT_TABLE()
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Thread completion event not being picked up in handler

Post by doublemax »

Try:

Code: Select all

EVT_THREAD(wxID_ANY, CheckForUpdateDialog::OnThreadCompletion)
Use the source, Luke!
sw
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sat Mar 16, 2019 8:09 pm

Re: Thread completion event not being picked up in handler

Post by sw »

That unfortunately had no effect... The thread completion is still not being invoked.

Just as an update, I did modify the Entry function to be like so:

Code: Select all

wxThread::ExitCode CheckForUpdateThread::Entry()
{
    std::string eventString = "";
    wxLogDebug("Entered thread");

    if (TestDestroy()) {
        return nullptr;
    }

    std::string githubUrl = "https://api.github.com";
    std::string releasesApi = "/repos/ifexception/wx-tasks-tracker/releases/latest";
    std::string url = githubUrl + releasesApi;

    wxLogDebug("About to make GET call");
    auto response = cpr::Get(cpr::Url{ url }, cpr::Header{ { "Accept", "application/vnd.github.v3+json" } });
    wxString res(response.text);
    wxLogDebug(res);

    if (response.status_code == 200) {
        auto jsonText = json::parse(response.text);
        auto name = jsonText["name"].get<std::string>();
        auto description = jsonText["description"].get<std::string>();
        auto htmlUrl = jsonText["html_url"].get<std::string>();

        auto currentVersion = "v" + common::GetVersion();
        if (name != currentVersion) {
            eventString = name + "," + description + "," + htmlUrl;
        }
    }

    wxLogDebug("Completed thread work");

    auto event = new wxThreadEvent(CHECK_UPDATE_THREAD_COMPLETED);
    event->SetString(eventString);
    wxQueueEvent(pHandler, event);
    wxLogDebug("Completed queueing event to handler");

    return (wxThread::ExitCode) 0;
}
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Thread completion event not being picked up in handler

Post by doublemax »

Replace:

Code: Select all

EVT_THREAD(wxID_ANY, CheckForUpdateDialog::OnThreadCompletion)
With (in CheckForUpdateDialog ctor):

Code: Select all

Bind(CHECK_UPDATE_THREAD_COMPLETED, &CheckForUpdateDialog::OnThreadCompletion, this );
Attachments
minimal.cpp
(8.34 KiB) Downloaded 57 times
Use the source, Luke!
sw
Experienced Solver
Experienced Solver
Posts: 56
Joined: Sat Mar 16, 2019 8:09 pm

Re: Thread completion event not being picked up in handler

Post by sw »

Oh cool, that worked! Thank you!

Do you perhaps know why it works with Bind and not with the macro (EVT_THREAD)?
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Thread completion event not being picked up in handler

Post by doublemax »

CHECK_UPDATE_THREAD_COMPLETED is a new event type, just like wxEVT_THREAD. EVT_THREAD would only match events of that type, but not events of type CHECK_UPDATE_THREAD_COMPLETED.

E.g. if you would change:

Code: Select all

auto event = new wxThreadEvent(CHECK_UPDATE_THREAD_COMPLETED);
to:

Code: Select all

auto event = new wxThreadEvent();
the old code (with the change regarding the id, wxID_ANY) should have worked.
Use the source, Luke!
Post Reply