views:

70

answers:

2

Hi! I have an XNA application, and I need to redirect the input queue into a custom thread, instead of having it available only in the main thread. Is there an alternative to AttachThreadInput?

+1  A: 

I did some searching on this, and I don't think you're going to find a great way to solve this. This post indicates that it may be possible if you "make a new input class, register those events in my games main thread, then start the thread to begin polling."

The general consensus from these two threads (including the one you started on the XNA forums) indicates to me that trying to send keyboard input to a different thread probably isn't the best idea, and that, if possible, the main thread should just handle the keyboard input and the other thread can read the input from the main thread's shared storage. An alternative would be the main thread telling the secondary thread to do certain functions based on what input it received.

Keyboard access from other thread
Keyboard Input on Another Thread

Venesectrix
One of those threads is actually mine (giuseppemag), I was hoping the broader audience of SO might add something :)
Giuseppe Maggiore
A: 

I'm not quite sure what you're asking, but I'll try to answer.

If you're trying to create a multi-player game and want input for each player to be handled by a thread you have to do the following:

  • Create the XNA objects related to Keyboard/Mouse/Gamepads in the main execution thread of your application

  • Pass the objects by reference to your custom input handling thread.

Threads share memory with the processes that spawn them, so any changes made to the object from inside your custom thread will be automatically accessible outside the thread by using your referenced object.

Hope this helps.

Seth Ellison
That is not the case, I am just building a game where updating and rendering are actually separate loops: updating is done in another thread, while drawing remains in the main thread.I need to read the input inside the updating thread, to compare readings relative to the updating loop iterations. Synchronizing input reads from the main thread would add so much overhead as to completely defeat the performance gain of having two threads...
Giuseppe Maggiore