Modern C++ Won't Save Us

evilrat evilrat666 at gmail.com
Fri Apr 30 17:27:57 UTC 2021


On Friday, 30 April 2021 at 16:36:26 UTC, Ola Fosheim Grøstad 
wrote:
>
> I found it interesting that several people want async/await in 
> D. I have only limited experience with async/await, but people 
> seem to be enthusiastic about it.
>

Maybe it would be cool, but I have no idea how to use it. I'm 
still using dlangui, and this means it has to be build on top of 
dlangui event loop, or dlangui moved to new standard, I also had 
vibe.d/dlangui mixed app as well, where should I put event loop 
now? The good thing, vibe.d has accounted for this (handle 
messages function to manually process messages from external 
event loop).
Same with existing solutions, I kind of want it, but 
unfortunately limited to something like this copy pasted all over 
my app...

```d

     void handleDrop(string[] files) {
       import std.file;
       auto paths = files.filter!(exists).array;
       paths.each!(path => _images[path] = null);

       if (paths.length < 1)
         return;

       // async, yay
       auto loadTask = task!( (path) {
         App.instance.onImageLoaded(path, loadImage(path));
       })(paths[0]);
       loadTask.executeInNewThread();
     }


     private void onImageLoaded(string path, ColorDrawBuf data) {
       _images[path] = data;
       setCurrentImage(path);
     }


     void setCurrentImage(string file) {
         _activeImage = file;
         // execute on next gui event
         imageView.executeInUiThread({
         imageView.setImage(_images[file]);
       });

       // even more async, yay \0/
       auto detectorTask = task!((s) {
         opencv.run_objectedetector(s);
         App.instance.onDetectFinished();
       })(detector);
       detectorTask.executeInNewThread();
     }


     private void onDetectFinished() {
       dstring text;
       foreach(det; detector.detections){
         text ~= format!"%s [%s%%] @%s\n"d(det.label, det.accuracy 
* 100f, det.rect);
       }
       // update text on next gui event
       editBox.executeInUiThread({
         editBox.text = text;
         imageView.invalidate();
       });
     }

```


More information about the Digitalmars-d mailing list