Background thread, async and GUI (dlangui)

GrimMaple grimmaple95 at gmail.com
Wed Jul 6 13:12:48 UTC 2022


On Wednesday, 6 July 2022 at 09:26:35 UTC, Bagomot wrote:
> Hello! I have a few questions about multithreading, asynchrony 
> and how it works with the GUI.
>
> 1) How to make asynchronous HTTP requests with curl in order to 
> receive progress and status? And how do you know that the 
> request is completed? This question is related to the next one, 
> so it should be answered in the context of working with the GUI.
>
> 2) How to get and update information in the GUI (I use dlangui) 
> about the execution of an asynchronous http request (for 
> example, downloading a file)? Something like a progress bar.
>
> 3) My application must perform some work in a separate thread 
> (I do this through inheritance of the worker from 
> core.thread.Thread, is that correct?). I need to be able to 
> manage data in the GUI from this thread without blocking GUI. I 
> found that you can use RunnableEvent for dlangui, but I can’t 
> figure out how to do it (and I didn’t find any examples). Could 
> you suggest how to do it?
>
> Sorry if the questions here are a bit from different topics, 
> but they are all related to dlangui.

I can't say anything about making asynchronous requests, but in 
dlangui simply updating any of the properties should cause a 
redraw. This means, if you set a new value to a progress bar, 
there isn't much else to do. Ideally, your library will provide a 
callback in which you can update anything you need.

If you don't receive a callback from your requests lib, you can 
poll it using timers in dlangui. For that you'd need to subclass 
any widget and override the `onTimer` method:
```d
class CustomCanvas : CanvasWidget
{
     this(string ID = null)
     {
         super(ID);
     }

     override bool onTimer(ulong id)
     {
         window.showMessageBox("OnTimer"d, "OnTimer"d);
         return true;
     }
}
```

And then set a timer in your code somewhere
```d
auto window = ...
auto canvas = new CustomCanvas("canvas");
window.setTimer(canvas, 1000); // will call onTimer every 1000ms
```


More information about the Digitalmars-d-learn mailing list