www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to spawn a thread within a GtkD button event handler

reply Alaindevos <devosalain ymail.com> writes:
Because when the eventhandler takes to much time the application 
is no longer responsive.
And even a simple redraw request is not performed before ending 
of the thread.
A small and short demo app would nice. Or guideline and direction.
Oct 08 2020
next sibling parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Thursday, 8 October 2020 at 15:59:15 UTC, Alaindevos wrote:
 Because when the eventhandler takes to much time the 
 application is no longer responsive.
 And even a simple redraw request is not performed before ending 
 of the thread.
 A small and short demo app would nice. Or guideline and 
 direction.
If I understand your situation, you want to modify widgets from a spawned thread. You cannot do that, but you should send a widget-state-change request to the main thread using Idle class. import glib.Idle; void aThreadFun(){ new Idle(delegate bool(){ // Modify your widget here. Redraw etc. return false; }); }
Oct 08 2020
parent reply Alaindevos <devosalain ymail.com> writes:
One thing I want to do is in an eventhandler of a button released 
event which takes minutes in duration to change the state of the 
statusbar indicating something is going on.
But the statusbar is not redrawn before the evenhandler finishes.

Better would be to start a new thread but D-spwawn-threads can 
not call member functions of the MainWindow calls so some 
plumbing with gtk is needed. This thread would coexist with the 
gtk main eventloop. The GTK docs on this look overwhelmingly 
complicated at first.
Oct 08 2020
parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Thursday, 8 October 2020 at 17:02:55 UTC, Alaindevos wrote:
 One thing I want to do is in an eventhandler of a button 
 released event which takes minutes in duration to change the 
 state of the statusbar indicating something is going on.
 But the statusbar is not redrawn before the evenhandler 
 finishes.

 Better would be to start a new thread but D-spwawn-threads can 
 not call member functions of the MainWindow calls so some 
 plumbing with gtk is needed. This thread would coexist with the 
 gtk main eventloop. The GTK docs on this look overwhelmingly 
 complicated at first.
I am typing on my mobile phone, so cannot give you a whole example. Just copied and pasted some existing code of mine. İnherit a Thread class: module downloadservice; import core.thread; import std.stdio; import appwindow;// your window class class DownloadService : Thread { property bool workingProperty() { return is_working; } // read property property bool workingProperty(bool value) { return is_working = value; } // write property AppWindow ctx;// access all members of your appwindow string itag, path, uuid; this(AppWindow _ctx, string _itag, string _path, string _uuid){ ctx = _ctx; itag = _itag; path = _path; uuid = _uuid; workingProperty = false; super(&run); } private: bool is_working; public: void run(){ if(this.workingProperty == false){ this.workingProperty = true; this.ctx.yvid.downloadItem(itag, path, uuid); this.workingProperty = false; } } } ... // İn your app window class // You don't have to use a thread pool pool = new ThreadPool((wkr, ctx){ auto worker = cast(DownloadService)wkr; worker.run(); }, cast(void*)this, 50, false);
Oct 08 2020
prev sibling parent reply Kagamin <spam here.lot> writes:
See 
https://forum.dlang.org/post/kqvpjwbkpravywaldiof forum.dlang.org
Oct 08 2020
parent reply Alaindevos <devosalain ymail.com> writes:
I created blindly a thread and this allowed to do stuff in 
background.
But when i passed the mainwindow as argument and did some stuff 
it resulted in,
[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and 
XInitThreads has not been called
[xcb] Aborting, sorry about that.
Assertion failed: (!xcb_xlib_threads_sequence_lost), function 
poll_for_event, file xcb_io.c, line 263.
Program exited with code -6
Oct 08 2020
next sibling parent reply Alaindevos <devosalain ymail.com> writes:
The idea looked good but gtk was unhave when 1000 labels where 
updated by another thread.
Oct 08 2020
parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Thursday, 8 October 2020 at 19:57:12 UTC, Alaindevos wrote:
 The idea looked good but gtk was unhave when 1000 labels where 
 updated by another thread.
Are you using Idle? If yes, and still having issues? Sorry, I cannot help further.
Oct 08 2020
parent reply Alaindevos <devosalain ymail.com> writes:
No use geany. The error occurs also when no editor is open.
I think the gtk libraries are not meant to be used in 
multithreaded way, where gtk is not knowing what is happening.
A process in background doing file things is no problem.
It is when a process in background tries to update a lot of 
widgets.
Probably the internal event queue of gtk gets confused.
Oct 08 2020
parent reply Alaindevos <devosalain ymail.com> writes:
I'll try your example. Thanks.
Oct 08 2020
parent reply Alaindevos <devosalain ymail.com> writes:
I did not used glib.idle. Probably this is essential.
Oct 08 2020
parent Alaindevos <devosalain ymail.com> writes:
With idle (not the editor :)) , it works perfect. Thanks for your 
help.
Oct 08 2020
prev sibling parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Thursday, 8 October 2020 at 19:53:26 UTC, Alaindevos wrote:
 I created blindly a thread and this allowed to do stuff in 
 background.
 But when i passed the mainwindow as argument and did some stuff 
 it resulted in,
 [xcb] Unknown sequence number while processing queue
 [xcb] Most likely this is a multi-threaded client and 
 XInitThreads has not been called
 [xcb] Aborting, sorry about that.
 Assertion failed: (!xcb_xlib_threads_sequence_lost), function 
 poll_for_event, file xcb_io.c, line 263.
 Program exited with code -6
I put a minimal example here: https://gist.github.com/aferust/2b469fad974bf8d80ede4db0d0627645
Oct 08 2020