Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:20:44+00:00 2026-06-13T01:20:44+00:00

I just started learning .NET so chances are this is a big n00b mistake:

  • 0

I just started learning .NET so chances are this is a big n00b mistake:

I’m trying to make a simple pong game, using a form and then the
System::Drawing::Graphics class to draw the game to the form.

The significant bits of my code look like this:

(Main Game Loop):

void updateGame()
{
    //Update Game Elements
    (Update Paddle And Ball) (Ex. paddle.x += paddleDirection;)
    //Draw Game Elements
    //Double Buffer Image, Get Graphics
    Bitmap dbImage = new Bitmap(800, 600);
    Graphics g = Graphics::FromImage(dbImage);
    //Draw Background
    g.FillRectangle(Brushes::White, 0, 0, 800, 600);
    //Draw Paddle & Ball
    g.FillRectangle(Brushes::Black, paddle);
    g.FillRectangle(Brushes::Red, ball);
    //Dispose Graphics
    g.Dispose();
    //Draw Double Buffer Image To Form
    g = form.CreateGraphics();
    g.DrawImage(dbImage, 0, 0);
    g.Dispose();
    //Sleep
    Thread.sleep(15);
    //Continue Or Exit
    if(contineGame())
    {
        updateGame();
    } 
    else
    {
        exitGame();
    }
}

(Form Initialization Code)

void InitForm()
{
    form = new Form();
    form.Text = "Pong"
    form.Size = new Size(800, 600);
    form.FormBorderStyle = FormBorderStyle::Fixed3D;
    form.StartLocation = StartLocation::CenterScreen;
    Application::Run(form);
}

PS. This is not the exact code, I just wrote it from memory, so that’s where any typos or
wrong names, or some important lines of code to do with initializing the form would be coming from.

So that is the code.
My issue is is that the game is certainly not updating every 15 milliseconds (about 60 fps), its going much slower, so what I have to do instead is move the paddle/ball larger amounts of distance each time to compensate for it not updating very quickly, and that looks very bad.

In a nutshell, something when it comes to drawing the graphics is slowing the game down a very large amount. I have a feeling it has something to do with me double buffering, but I cannot get rid of that, since that would create some yucky flickering. My question is,
how do I get rid of this lag?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:20:45+00:00Added an answer on June 13, 2026 at 1:20 am

    There are several issues with this code that can heavily impact app performance:

    1. Creating a large Bitmap buffer every frame and not disposing it
    2. Implementing double-buffering while WinForms already have a great implementation of this behavior
    3. updateGame() is recursive but it doesn’t have to be recursive
    4. Calling Thread.Sleep() in GUI thread

    Code sample, which uses separate thread to count game ticks and WinForms built-in double-buffering:

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new GameWindow());
        }
    
        class GameWindow : Form
        {
            private Thread _gameThread;
            private ManualResetEvent _evExit;
    
            public GameWindow()
            {
                Text            = "Pong";
                Size            = new Size(800, 600);
                StartPosition   = FormStartPosition.CenterScreen;
                FormBorderStyle = FormBorderStyle.Fixed3D;
                DoubleBuffered  = true;
    
                SetStyle(
                    ControlStyles.AllPaintingInWmPaint |
                    ControlStyles.OptimizedDoubleBuffer |
                    ControlStyles.UserPaint,
                    true);
            }
    
            private void GameThreadProc()
            {
                IAsyncResult tick = null;
                while(!_evExit.WaitOne(15))
                {
                    if(tick != null)
                    {
                        if(!tick.AsyncWaitHandle.WaitOne(0))
                        {
                            // we are running too slow, maybe we can do something about it
                            if(WaitHandle.WaitAny(
                                new WaitHandle[]
                                {
                                    _evExit,
                                    tick.AsyncWaitHandle
                                }) == 0)
                            {
                                return;
                            }
                        }
                    }
                    tick = BeginInvoke(new MethodInvoker(OnGameTimerTick));
                }
            }
    
            private void OnGameTimerTick()
            {
                // perform game physics here
                // don't draw anything
    
                Invalidate();
            }
    
            private void ExitGame()
            {
                Close();
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                var g = e.Graphics;
                g.Clear(Color.White);
    
                // do all painting here
                // don't do your own double-buffering here, it is working already
                // don't dispose g
            }
    
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                _evExit = new ManualResetEvent(false);
                _gameThread = new Thread(GameThreadProc);
                _gameThread.Name = "Game Thread";
                _gameThread.Start();
            }
    
            protected override void OnClosed(EventArgs e)
            {
                _evExit.Set();
                _gameThread.Join();
                _evExit.Close();
                base.OnClosed(e);
            }
    
            protected override void OnPaintBackground(PaintEventArgs e)
            {
                // do nothing
            }
        }
    }
    

    Even more improvements can be made (for example, invalidating only parts of game screen).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just recently started learning and using the ADO.NET Entity Framework and ran into
I just started learning ASP.NET MVC4 today. After reading tutorials, downloading VS 2012, and
I've just started learning ASP.net and I can't seem to get the GridView to
I just started learning C but I don't understand this part of the code.
I´ve just started learning GWT and I´m trying to implement http://gwt.google.com/samples/Showcase/Showcase.html#!CwFileUpload and but failing
I just started learning Common Lisp a few days ago, and I'm trying to
I`m just started learning C#, used to be a VB programmer. In VB.NET, it
I am just learning Blend/Silverlight/VS2010/.net/etc. I have a simple project that resides on a
I have just started learning asp.net mvc and one reason of the primary reasons
I've just started learning F# (with little prior experience with .NET) so forgive me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.