Threading in c#

Started by
2 comments, last by binchawpz 15 years, 6 months ago
Hi, After implementing a game state system based on the one fromt he xna creators website, I decided to expland the laoding screen a little bit by animating something that was being drawn on the screen while things are being loaded int he background. Without any experience with threading before I decided this was the best way to go, so ill start with posting some code:

Thread renderThread = new Thread(DrawProxy);
renderThread.Start();

foreach (XGameScreen screen in screensToLoad)
     if (screen != null)
            ScreenManager.AddScreen(screen);

renderThread.Abort();
ScreenManager.RemoveScreen(this);

 void DrawProxy()
        {
            Draw(new GameTime());
        }


Probably horrible i know, but bear with me =] The idea was i create a thread which keeps simply calling draw (draw increments the angle of rotation of a sprite and then draws it to the screen) which the main thread loads each game screen, once all the game screens are loaded the thread is removed and the game carrys on. This all works fine, until i decided to add something silly in jsut to see what happens during a long load, so i simply added a loop into the loading method of one of the screens:


            for(int i = 0; i < int.MaxValue; ++i)
            {
                int j = 1;
            }

As you see, completly pointless, but it takes a long[ish] time to execute. The results were that the sprite rotated until the main program gets to this loop, at which point the sprite stops rotating, and it appears that the thread is no longer working. Am I missing something I should be doing here? Or is this simply a by product of small pointless loops like the one I have? Thanks, Scott
Game development blog and portfolio: http://gamexcore.co.uk
Advertisement
In your specific example, DrawProxy will only be called once. It might be easier to just do the rendering in the foreground and all the screen loading in the background. That way all your rendering is in one thread and you can keep track of what is accessed easier.

Regardless, here is a great resource to peruse before delving into multithreading with C#.
That is actually the exact article I read the top bit of before thinking "ill quickly write something, get some sleep then rea the rest"

I am onfused however that you say the draw function should only run once, since i can see the image rotate therefore it seems to be running more then once?

And I agree, the loading should definatly be taken care in the new thread as opposed to the drawing, the only reason i havnt done it that way around is I simply wanted to get the basics down first before I started messing around with a restructure on how the loading mechanism works =]

Thanks tho, ill take you linking that as an "i really should read it before continuing", tomoros job I think.

Scott
Game development blog and portfolio: http://gamexcore.co.uk
The Start method of the thread class calls the method you have attached to the Thread object via the constructor in a new thread. In other words, it won't do any looping. So what it will do in your case is call the DrawProxy method once. That will in turn call the Draw method once with a new GameTime object as an argument.

Keep in mind I have no idea what your Draw method does or how it will react to your GameTime object.

Threads on their own do not loop. They run until the attached method is done or they are aborted. Also, it is probably a bad idea to create a thread you have to abort (which is another reason loading in a background thread is the way to go). There is some information on the MSDN page for Thread's Abort method.

Basically read through that article and you'll probably have a much better sense of how to put these things together. Multithreading can mess with your head so it is a good idea to plan it out before you dive in. Experimenting is a great way to find out some of the peculiarities however.

Have fun.

This topic is closed to new replies.

Advertisement