Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem using separate thread to handle events  (Read 2653 times)

0 Members and 1 Guest are viewing this topic.

tchau83

  • Newbie
  • *
  • Posts: 2
    • View Profile
Problem using separate thread to handle events
« on: October 03, 2010, 06:40:14 am »
I'm coding a simulation program which displays the results in a window. The main thread handles the rendering while another thread handles the events. However the sf::Window function, global.app.WaitEvent() never returns, regardless of the event.

Code: [Select]
int main(){

   global.app.Create(...);
   sf::Thread thread(&getEvents);
   thread.Launch();

   while(not finished){
      ...
      glBegin()/glEnd()
      global.app.Display()
      ...
   }

   return;
}

void getEvents(){

   sf::Event event;
   while (global.app.WaitEvent(event)){

      if (event.Type == sf::Event::Closed)
         break;
   }
}


I tried adding global.app.SetActive() and sf::Context con to getEvents(), but it doesn't work either.

Can anyone help me get this to work? Thanks in advance


Side note:
I do not need event handling for the window, but the window will hang if it loses focus, even though the program is still running fine. So I think I need to clear the event stack periodically. I don't think I can put event handling in the main thread's loop, since each iteration takes several seconds to complete (the long period will probably trigger the window hang also). Also, this is for Windows XP. When running on Windows 7, the window hang problem doesn't occur even without event handling coded in.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem using separate thread to handle events
« Reply #1 on: October 03, 2010, 11:04:53 am »
You can't handle events in a thread other than the one where the window was created. This is a limitation of the OS.
Laurent Gomila - SFML developer

tchau83

  • Newbie
  • *
  • Posts: 2
    • View Profile
Problem using separate thread to handle events
« Reply #2 on: October 03, 2010, 01:18:12 pm »
I see.. I thought it could be done because in in the documentation for sf::Window::WaitEvent (SFML version 2), it is stated that "This function is typically used when you have a thread that is dedicated to events handling". Which I assume to mean a different thread for the same window.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem using separate thread to handle events
« Reply #3 on: October 03, 2010, 01:41:34 pm »
This is possible, but the window must be created in the same thread.
Laurent Gomila - SFML developer