How to spawn a thread within a GtkD button event handler

Ferhat Kurtulmuş aferust at gmail.com
Thu Oct 8 17:30:27 UTC 2020


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);



More information about the Digitalmars-d-learn mailing list