adamdruppe / arsd

This is a collection of modules that I've released over the years. Most of them stand alone, or have just one or two dependencies in here, so you don't have to download this whole repo.
http://arsd-official.dpldocs.info/arsd.html
530 stars 127 forks source link

callLater for thread safety #270

Closed aferust closed 3 years ago

aferust commented 3 years ago

Dear Adam,

I asked if minigui is thread-safe, and you said that you don't know almost a year ago. I implemented it please do your tests.

import std.concurrency;

import arsd.minigui;

LineEdit text1;

void main() {
    auto window = new MainWindow();

    auto exitAction = new Action("E&xit");
    exitAction.triggered ~= { window.close(); };

    window.statusTip = "D ROX!";

    auto vl = new VerticalLayout(window);

    text1 = new LineEdit(vl);
    auto button1 = new Button("Hmm", vl);

    button1.addEventListener(EventType.triggered, () {
        spawn((){
            // callLater(delegate(){ // uncomment me for thread safety
                text1.content = "Hmm";
            // }); // uncomment me for thread safety

        });

    });

    window.loop();
}
adamdruppe commented 3 years ago

Actually since then I made something better... you can

http://dpldocs.info/experimental-docs/arsd.simpledisplay.runInGuiThread.html

runInGuiThread now!

0xEAB commented 3 years ago

Did you know you can use UFCS on delegate literals?

neat :)

aferust commented 3 years ago

Actually since then I made something better... you can

http://dpldocs.info/experimental-docs/arsd.simpledisplay.runInGuiThread.html

runInGuiThread now!

Hey c'mon. You took my chance for contributing :-D.

adamdruppe commented 3 years ago

yeah this came up because i wanted to do the integrated terminal emulator thing in terminal.d... and to retrofit old terminal code, i put the gui in a separate thread. (which i now kinda regret, i should have just had them communicate through a pipe but meh oh well)

this meant i needed some way to run stuff like menus in the appropriate thread and thus that little function was born. kinda hacky but seems to work.