.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Windows Application >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Multithreaded Animation Application

Posted By:Jean Paul       Posted Date: October 05, 2010    Points: 50    Category: Windows Application    URL: http://www.dotnetspark.com  

Cheers Guys.. In this application I would like to demonstrate the power of threading through an animation of football
 

Introduction

We have heard that in Games multithreading will be implemented.  But what are the advantages of using Multithreading.  To start with I would like to show the disadvantages of single threaded gaming application.

Single Threaded Application

Here only one thread will be there to manage the UI objects.  In a game like multiple objects moving over the screen it will require tremendous coding effort to manage all the objects.  You can see in the application the existing animations are paused while adding a new animated object.

Multi Threaded Application

Here each animate object will be executed in separate threads.  So decoupling of responsibilities is possible and provides a cleaner code.  All the animated objects continues in the state of running.

Button1 :: Add Ball

Here balls are added using the same main thread.  The previous animation would get blocked while adding each ball.

Button2 :: Add ball (with multithreading)

Here balls are added in separte threads.  All the balls will be continuing in its state of animation

Code Explained

Here in the main form there is a panel which will serve as the play ground for the football (animated object).

There are 2 buttons to add balls to the panel.

Class :: Football

Football is the main class handling all the ball movements.  The Start() method is called for starting the animation.  The same method is called from the non-threaded/threaded enviornments.  The Start() method employs an infinite while loop to carry over the animation continuously.  Test the "Add Ball" buttons separately after running two instances of the application.

The constructor of class Football takes 2 argumetns, the first one is playground of type Control.  Here we are actually passing the Panel on main form.  The second argument is of type Image which is the football picture stored in the Images.resx.

The initial location (left and top) of football is randomly chosen using the instances _randomX and _randomY respectively.

For creating a new football instance the code is given below:

_controller = new Football(panel1, Images.Football);
_controller.Start();

From a multithreaded perspective the code will look like:

_controller = new Football(panel1, Images.Football);

ThreadStart tstart = new ThreadStart(_controller.Start);
Thread thread = new Thread(tstart);
thread.Start();

Running the application

Download the zip file, extract it and open the MultithreadingBallGameMultithreadingBallGame.sln file and execute.

 

Cheers.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend