Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to run winapi.MessageLoop() asyncronously? #32

Open
pluto439 opened this issue Oct 18, 2017 · 4 comments
Open

Is it possible to run winapi.MessageLoop() asyncronously? #32

pluto439 opened this issue Oct 18, 2017 · 4 comments
Labels

Comments

@pluto439
Copy link

As it is now, repl terminal gets blocked when I launch winapi.MessageLoop(). Is there a way to start it asyncronously?

Here is the code I'm using. The only difference from one on docs page is lack of local, it doesn't work in repl for some reason.

winapi = require'winapi'
require'winapi.windowclass'

win = winapi.Window{
   w = 500,                --all these are "initial fields"
   h = 300,
   title = 'Lua rulez',
   autoquit = true,        --this is to quit app when the window is closed
   visible = false,        --this field is from BaseWindow
}

function win:on_close()    --this is an event handler
   print'Bye'
end

print(win.title)           --this is how to read the value of a property
win.title = 'Lua rulez!'   --this is how to set the value of a property
win:show()                 --this is a method call

winapi.MessageLoop() --start the message loop

I want to do win.w=100 after the window is opened. From terminal.

@capr
Copy link
Member

capr commented Oct 19, 2017

What exactly do you mean by asynchronously? In a different thread?

@pluto439
Copy link
Author

Probably, but I'm afraid having two threads will complicate things...

I need to get window running, but still have console available to manipulate it.

@capr
Copy link
Member

capr commented Oct 19, 2017

You'll have to implement that yourself somehow. Either with threads and queues or with coroutines and a custom repl (i.e. not one that is blocking waiting for key presses).

@Rami-Sabbagh
Copy link

Rami-Sabbagh commented Aug 5, 2018

It's kind of possible, (I've done it).

You'll have to handle the messages loop yourself, it's very resourceful to look at the messages loop code in https://github.com/luapower/winapi/blob/master/winapi/basewindowclass.lua

Functions to look for in basewindowclass.lua:

  • ProcessMessage
  • MessageLoop
  • ProcessNextMessage (Most important, has a timeout parameter).
  • ProcessMessage (Use MessageLoop instead, this doesn't return the exit code).

Here's some code I've written to show a window for 10 seconds then close it:

  win.visible = true
	
  local endClock = os.clock() + 10
  
  while true do
    local continue, exitcode = winapi.ProcessNextMessage(math.max(endClock-os.clock(),0))
    
		--Window has been closed manually.
    if not continue and exitcode then break end
    
    --Time over, close the window and confirm that.
    if os.clock() >= endClock then
      win:close()
      winapi.MessageLoop()
      break
    end
  end

I hope my comment was useful even after 1 year of the issue creation.

@capr capr added the question label Aug 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants