I'm trying to use threads because I want to write messages in textArea and 
these messages are generated by a proc called by click a button:
    
    
    import nigui
    import std/strformat
    import os
    
    var ch: Channel[string]
    
    proc messaggiInTextArea(primoMsg: string) {.thread.} =
        ch.send(primoMsg)
        for x in 1..10:
            ch.send(&"Messaggio n°: {x}")
            sleep(1000)
        ch.send("Dopo il ciclo for")
        ch.send("")
    
    app.init()
    
    var window = newWindow("Test Thread")
    
    var container = newLayoutContainer(Layout_Vertical)
    window.add(container)
    
    var buttonSave = newButton("Esegui test")
    container.add(buttonSave)
    
    var areaMessaggi = newTextArea()
    areaMessaggi.editable=false
    container.add(areaMessaggi)
    
    buttonSave.onClick = proc(event: ClickEvent) =
        var th: Thread[string]
        areaMessaggi.text=""
        areaMessaggi.addLine("Inizia il test: prima della chiamata alla 
procedura")
        ch.open()
        createThread(th, messaggiInTextArea, "Prima chiamata dalla proc")
        while true:
            let msg = ch.recv()
            if msg == "":
                break
            areaMessaggi.addLine(msg)
        joinThreads(th)
        ch.close()
        areaMessaggi.addLine("Fine del test: dopo la chiamata alla procedura")
    
    
    window.show()
    
    app.run()
    
    
    Run

the result it is that all messages are written in the same time to the end the 
proc.

Since I'm not very expert in threads, it will be clear to most of you why what 
I've done doesn't work.

Could you explain why and give me an alternative solution?

Thank you all

Reply via email to