Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
How can update UI from other thread — Gideros Forum

How can update UI from other thread

phuochauphuochau Member
edited June 2012 in General questions
Hi all,
I'm facing with the problem. I have a simple TCP server (C#).
And i create new tcp socket at client, but when i get a new message (the message is a string) from server, i cannot update UI. Please help me

Comments

  • Can you explain your problem in a little more detail?
    What do you mean "I cannot update UI" ?

    Likes: phuochau

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
    +1 -1 (+1 / -0 )Share on Facebook
  • phuochauphuochau Member
    edited June 2012
    @techdojo
    What I'm expecting is when client receives a string from server, client will set text for text field
  • ar2rsawseenar2rsawseen Maintainer
    so there are two things to be considered

    Firstly, when you create a client, set it's timeout to 0, so it won't block UI
    client:settimeout(0)
    Secondly, listen in timer and not in loop, since times are like async calls (they are not async, but they act like ones), listening about 10 times a second should be enough.
    --running timer 10 times each second should be sufficient
    local timer = Timer.new(100)
     
    timer:addEventListener(Event.TIMER, function()
    --do what you need to do here
    end)
     
    --start the timer
    timer:start()
    If that didn't answer your question, try elaborate on it ;)

    Likes: phuochau

    +1 -1 (+1 / -0 )Share on Facebook
  • @ar2rsawseen:
    Thanks, I will try it now :)
  • phuochauphuochau Member
    edited June 2012
    @ar2rswascreen
    Working ;), thanks for your support. Code complete such as below. I use 2 timers to do that.
    local socket = require("socket")
    local client = socket.connect("127.0.0.1", 1501)
    local count = 0
    client:settimeout(0)
     
    local timer = Timer.new(100) 
    timer:addEventListener(Event.TIMER, function()
    	print(count)
    end)
     
    local timer2 = Timer.new(100) 
    timer2:addEventListener(Event.TIMER, function()
    	s, status, partial = client:receive()
    	if partial~=nil then
    		count = count + partial:len()
    	end
    end)
     
    --start the timer
    timer:start()
    --start the timer
    timer2:start()

Sign In or Register to comment.