Chryso

Hello.

I am able to create thread and make them exchange information through common objects they know (server / each other reference)
Now I would like 2 threads to update a server.

Each of those threads takes for inputs a keyboard for one, a mouse for the other one. However, it seems that the thread that listens on the keyboard do not detect events from it. Seems like it is the parent thread that is able to listen to a keyboard event and only that parent thread.

How would you do so that a children thread is master over a device even if it does not have the focus

Regards,

Chryso


Re: XNA Framework Multi threading in a game (using XNA)

Ryan Lamansky

As far as I know (which isn't that far, really), XNA is designed for all XNA-based activity to happen on the main thread. You can certainly use other threads for background processing (and there are wonderous performance gains to be realized from doing this), but the XNA stuff has to all happen on the main thread.

-Ryan / Kardax





Re: XNA Framework Multi threading in a game (using XNA)

Chryso

Hello,

Thank you for the answer. Based on it, I am thinking of something weirder. Would it be possible to launch 2 "game" that would only listen to a keyboard / mouse and then transmit the info to another thread

Regards,

Chryso




Re: XNA Framework Multi threading in a game (using XNA)

Ryan Lamansky

Don't know, try it :-)

(I am doubtful, though...)

-Ryan / Kardax





Re: XNA Framework Multi threading in a game (using XNA)

Friendly Dog

I think there's a design decision to be made. What are you trying to do To allow to players to share one keyboard




Re: XNA Framework Multi threading in a game (using XNA)

Chryso

It is more like separating code.

The keyboard / mouse is just a start. Using gamepad is what comes next. And if I want to push it to an XBox, I might have up to 4 Gamepad to manage.

So, instead of having the video renderer manage keyboard inputs and some checkings about those inputs directly I was thinking of running a thread to manage a device that would communicate with the renderer.

And since the treatment required by the input might be heavy, splitting input management, and video rendering seemed like a pretty good idea.

Now, maybe, that is not how I should do it, but I do not really see any other way.
Also, splitting input management and video rendering in 2 threads is interesting for multicore CPUs.

Any input is interesting to have regarding this.

Regards,

Chryso




Re: XNA Framework Multi threading in a game (using XNA)

CalvinR

You seem to be going about this wrong, instead of writing specific input code for all different types of inputs I would first create generic input algorithms(methods), say a move, or a fire, or a select or whatever, then you simply call those input methods from the events generated by the keypresses/mouse events/gamepad events/etc.
And if you think about it input management shouldn't really be that computationaly intensive as even the fastest user is still not that fast when compared to the computer.
Now if you are mixing input and input processing then yes you'd probably want to have game logic processing done on a seperate thread to your video renedering as the two are probably going to be the most computationally intensive things your game will be performing.

Here are some excellent articles for game development.
http://www.gamedev.net/





Re: XNA Framework Multi threading in a game (using XNA)

abcdefgqwerty2

Maybe not the most helpful response but why dont you just ignore all the hassle of multi threading and not use it I mean multithreading is a ton of work for no real major gain as of today. I mean yeah maybe input or something easy can use a thread but multithreading the whole thing isnt even worth it yet. multicore is not common enough for the major pain it is.

If you keep things modular enough you could go through and add threads later if you desire.It depends on how complex a game you are talking. I wouldnt want to go through quake 3 and add in multi threading no matter how  modular it was.




Re: XNA Framework Multi threading in a game (using XNA)

Chryso

Well,
Simple answer :)

Computer Game in XNA that is multi threaded and works (thats the hard part hehe) makes for a pretty efficient game on a XBox 360 where you have 3 cores, and 2 Hardware Thread per Core :)

And I prefer to start using threading on a personal project where I can make all the mistakes I want than do it on my job. And you do have to start at a time.

More and more computer are sold with multiple cores. So I ll keep to threading.

Regards,
Chryso




Re: XNA Framework Multi threading in a game (using XNA)

Friendly Dog

As for the joystick/keypad/keyborad handling, you can create a new game using the XNA template project (it's called space race or something like that), it contains ALL the code you'll need to handle user inputs. When I was working on my XNA project I was able to copy the code over to my own project and it worked well with XBOX joysticks and keyboard.




Re: XNA Framework Multi threading in a game (using XNA)

Kyle_W

Multithreading does not always lead to better performance. You have to consider the interdependencies of your code. Multiple threads are best suited for long running tasks that can complete their work without depending on information from other tasks that are running concurrently. Here's another thread you may want to read:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1276365&SiteID=1





Re: XNA Framework Multi threading in a game (using XNA)

Bill Reiss

The XNA Racer Starter Kit (or whatever its official name is going to be) is going to be an example of taking advantage of threading and multiple cores on the Xbox. As far as I know, no release date has been set for this starter kit.




Re: XNA Framework Multi threading in a game (using XNA)

Chryso

Hello,
So I got the graphics in a different thread than the input management.
However, I am wondering about something.
How do I check that I really have 2 differents threads Any way to see in the task manager the threads (I know how to show all user process, but not all threads even children threads, is it even possible )

Regards,

Chryso




Re: XNA Framework Multi threading in a game (using XNA)

Chryso

Hello,

So I have the game thread that is processing inputs.
I have a thread that manages all graphics stuff.

However the thread that manages the graphic stuffs needs to access the Window. object.
So we tried using reference but it was not thread safe. (directly using thegame.window.... )

Then I tried using public method to retrieve the data as follow:
public float getWindowsHeight()
{
return Window.ClientBounds.Height;
}

Since this did not work I tried using a lock:
public float getWindowsWidth()
{
lock (locker)
{
return Window.ClientBounds.Width;
}
}

But still does not work.

I tried using delegate, but I am having a hardtime using it since I do not have the ability to use the InvokeRequired method. And I tried using the following code:

public float getWindowsWidth()
{
launchMethod d = new launchMethod(getWidth); // to disable recursivity since I did not have InvokeRequired()
d.Invoke();
}

private float getWidth() {
return Window.ClientBounds.Width;
}

But this does not work either.

Question: How do I make the call the one of the upper method to retrive the windows height / width in a thread safe manner

Regards,

Chryso






Re: XNA Framework Multi threading in a game (using XNA)

Shawn Hargreaves - MSFT

You can't access the window settings from a worker thread, because the Win32 API does not support threading for UI code.

You will need to look these up in your main thread, and then pass that in to the worker thread in some way.