{"id":4226,"date":"2022-02-05T05:53:37","date_gmt":"2022-02-05T05:53:37","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4226"},"modified":"2025-09-29T10:09:23","modified_gmt":"2025-09-29T10:09:23","slug":"13c-wxwidgets-multithreading","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/02\/05\/13c-wxwidgets-multithreading\/","title":{"rendered":"13c. wxWidgets &#8211; Multithreading"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>Multi-threading is a complex topic and it requires a good amount of usage before one can get familiar with it. Here we are only going to deal with some basic handling of threads. By default everything in wxWidgets is handled on the main GUI thread. If you try to do some time consuming process on the main thread, it will block up the UI and the application will freeze for the time that the process is active.<\/p>\n\n\n\n<p>The correct way to handle long running tasks is to run them on a different thread. That way, the main GUI thread is kept free and the application remains responsive.<\/p>\n\n\n\n<p>Another thing to deal with is the updation of the UI from another thread. It is not recommended to try and update anything on the UI from a non-GUI thread. Anyway , any wxWidget object which has been created on the main GUI thread cannot be manipulated from another thread. The correct way to update the UI from another thread is to send an event message to the main thread.<\/p>\n\n\n\n<p>In the sample code below. we subclass a wxThread class and we run a simple loop from which we update the main GUI after each iteration of the loop. We have added a <em>Sleep()<\/em> method so that the thread executes slowly. The main point to notice here is that while the thread is running, the wxFrame is still responsive and you can move it around or resize it. <\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">testthread.h<br><\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n#include &lt;wx\/wx.h&gt;\n#endif \/\/ WX_PRECOMP\n\nclass TestThread:public  wxThread {\n  public:\n      TestThread(wxFrame *parent);\n      virtual ~TestThread();\n      virtual void *Entry();\n\n  private:\n    int mCount;\n    wxFrame *mParent;\n    void sendMessage(wxString message);\n};\n\nconst int  EVENT_UPDATE_UI = 101;\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">testthread.cpp\n<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;testthread.h&quot;\n\nTestThread::TestThread(wxFrame *parent): wxThread() {\n    mCount = 0;\n    mParent = parent;\n}\n\nTestThread::~TestThread() {\n}\n\nwxThread::ExitCode TestThread::Entry() {\n    sendMessage(wxT(&quot;Thread started.\\n&quot;));\n\n    for (int count=0; count &lt; 10; count++) {\n        if (TestDestroy()) {\n            break;\n        }\n        sendMessage(wxString::Format(&quot;Thread no.%d\\n&quot;, count));\n        wxThread::Sleep(1000);\n    }\n    sendMessage(wxT(&quot;Thread completed.\\n&quot;));\n    return NULL;\n}\n\nvoid TestThread::sendMessage(wxString message) {\n    wxThreadEvent event(wxEVT_THREAD, EVENT_UPDATE_UI);\n    event.SetString(message);\n    mParent-&gt;GetEventHandler()-&gt;AddPendingEvent(event);\n}\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">runthreadtest.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n#include &lt;wx\/wx.h&gt;\n#endif \/\/ WX_PRECOMP\n\nclass RunThreadTest:public wxFrame {\n  public:\n      RunThreadTest(const wxString &amp;title);\n      void onUIUpdate(wxThreadEvent &amp;event);\n\n  private:\n      wxTextCtrl *mText;\n};\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">runthreadtest.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;runthreadtest.h&quot;\n#include &quot;testthread.h&quot;\n\nRunThreadTest::RunThreadTest(const wxString &amp;title):\n    wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(700,400)) {\n\n    mText = new wxTextCtrl(this, -1, wxT(&quot;&quot;), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY);\n    this-&gt;Bind(wxEVT_THREAD, &amp;RunThreadTest::onUIUpdate, this);\n\n    TestThread *th = new TestThread(this);\n    if (th-&gt;Create() != wxTHREAD_NO_ERROR) {\n        wxPuts(&quot;Cannot create thread&quot;);\n        return;\n    }\n    if (th-&gt;Run() != wxTHREAD_NO_ERROR) {\n        wxPuts(&quot;Cannot  start thread&quot;);\n        return;\n    }\n\n    Center();\n}\n\nvoid RunThreadTest::onUIUpdate(wxThreadEvent &amp;event) {\n    mText-&gt;AppendText(event.GetString());\n    mText-&gt;AppendText(wxT(&quot;\\n&quot;));\n}\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n\nclass Main: public wxApp {\n\n\tpublic:\n\t\tvirtual bool OnInit();\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;runthreadtest.h&quot;\n\nIMPLEMENT_APP(MainApp)\n\nbool MainApp::OnInit() {\n    RunThreadTest *app = new RunThreadTest(wxT(&quot;Thread demo&quot;));\n    app-&gt;Show(TRUE);\n\n    return TRUE;\n}\n\n\n\n<\/pre><\/div>\n\n\n<p>The sample output is given below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"400\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-05-11-21-24.png\" alt=\"\" class=\"wp-image-4231\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-05-11-21-24.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-05-11-21-24-300x240.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW Multi-threading is a complex topic and it requires a good amount of usage before one can get familiar with it. Here we are only <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/02\/05\/13c-wxwidgets-multithreading\/\" title=\"13c. wxWidgets &#8211; Multithreading\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,365],"tags":[],"class_list":["post-4226","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4226"}],"version-history":[{"count":8,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4226\/revisions"}],"predecessor-version":[{"id":4697,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4226\/revisions\/4697"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3595"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}