5 Replies - 796 Views - Last Post: 04 May 2016 - 02:56 PM Rate Topic: -----

#1 ottodidact  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-May 16

Simple Windows Forms Application very slow during testing

Posted 04 May 2016 - 10:10 AM

I wrote a short, simple text adventure game in a Windows Form Application in Visual Studio Community 2015.

There are no images, no bells and whistles, no calls to a data base. There are no nested if statements. I tried to make this as simple as possible just using my limited C# .Net skills.

Tested individually, the code blocks return no errors.

But when I run through the whole thing (in Debug mode) the re-rendering slows to a crawl after about the 20th code block.

Below is the first part actual code. It is pretty much the same thing for the rest of the 1500 or so lines of code. The only thing that really changes is the content of the text, and the "if else" statements do increase in later code blocks (sometimes there are up to ten if else statements per code block).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Backbone
{
    public partial class DarkRoom : Form
    {

        public int trackball = 0;


        public DarkRoom()
        {
            InitializeComponent();
            button1.Visible = false;
            textBox1.Visible = false;
            trackball = 0;
            label1.Text = "Before you stands the old and ancient castle...";
            nextButton.Click += new EventHandler(this.nextButton_Click1);
        }


        private void nextButton_Click1(object sender, EventArgs e)
        {

            label1.Text = "How could this... ";
            nextButton.Click += new EventHandler(this.nextButton_Click2);
        }

        private void nextButton_Click2(object sender, EventArgs e)
        {

            label1.Text = "'Yet, where is...";
            nextButton.Click += new EventHandler(this.nextButton_Click2);
        }


        private void nextButton_Click2(object sender, EventArgs e)
        {
            textBox1.Visible = true;
            button1.Visible = true;
            nextButton.Visible = false;
            label1.Text = "In front of you is...";
            button1.Click += new EventHandler(this.button1_Click1);
        }


        private void button1_Click1(object sender, EventArgs e)
        {

            string user1Answer1 = textBox1.Text;

            user1Answer1 = user1Answer1.ToLower();

            if (user1Answer1 == "abracadabra") { YellowRoom(); }  /// this is to provide a shortcut during testing

            else if (user1Answer1 == "look altar") { Room2(); }

            else if (user1Answer1 == "use altar") { Room2(); }

            else { Room1(); }
        }


        private void Room1()
        {
            label1.Text = "You are in...";
            button1.Click += new EventHandler(this.button1_Click1);
        }

        private void Room2()
        {
            label1.Text = "Upon the altar...";
            button1.Click += new EventHandler(this.button1_Click2);
        }

        private void button1_Click2(object sender, EventArgs e)
        {
            string user1Answer1 = textBox1.Text;

            user1Answer1 = user1Answer1.ToLower();

            if (user1Answer1 == null) { Room2(); }

            else if (user1Answer1 == "take shield") { Room3(); }

            else if (user1Answer1 == "use shield") { Room3(); }

            else {
                Room2();
            }
        }

        private void Room3()
        {
            label1.Text = "Beneath...";
            button1.Click += new EventHandler(this.button1_Click3);
        }

        private void button1_Click3(object sender, EventArgs e)
        {
            string user1Answer1 = textBox1.Text;

            user1Answer1 = user1Answer1.ToLower();

            if (user1Answer1 == null) { Room3(); }

            else if (user1Answer1 == "take sword") { Room4(); }

            else if (user1Answer1 == "use sword") { Room4(); }

            else {
                Room3();
            }
        }

        private void Room4()
        {
            label1.Text = "Beneath... ";
            button1.Click += new EventHandler(this.button1_Click4);
        }

        private void button1_Click4(object sender, EventArgs e)
        {
            string user1Answer1 = textBox1.Text;

            user1Answer1 = user1Answer1.ToLower();

            if (user1Answer1 == null) { Room4(); }

            else if (user1Answer1 == "take mask") { Room5(); }

            else if (user1Answer1 == "use mask") { Room5(); }

            else {
                Room4();
            }
        }

        private void Room5()
        {
            label1.Text = "You don the... ";
            button1.Click += new EventHandler(this.button1_Click5);
        }

        private void button1_Click5(object sender, EventArgs e)
        {
            string user3Answer = textBox1.Text;

            user3Answer = user3Answer.ToLower();

            if (user3Answer == null) { Room5(); }

            else if (user3Answer == "use door") { blackRoom(); }

            else {
                Room5();
            }
        }

        private void blackRoom()
        {
            label1.Text = "You are in a dimly lit room...";
            button1.Click += new EventHandler(this.button1_ClickSarc1);
        }

        private void button1_ClickSarc1(object sender, EventArgs e)
        {
            string user4Answer = textBox1.Text;

            user4Answer = user4Answer.ToLower();

            if (user4Answer == null) { blackRoom(); }

            else if (user4Answer == "look sarcophagus") { sarcRoom2(); }

            else if (user4Answer == "open sarcophagus") { sarcRoom3(); }

            else if (user4Answer == "use mask") { sarcRoom9(); }

            else {
                blackRoom();
            }
        }

        private void sarcRoom2()
        {
            label1.Text = "The black sarcophagus...";
            button1.Click += new EventHandler(this.button1_ClickSarc2);
        }

        private void button1_ClickSarc2(object sender, EventArgs e)
        {
            string user3Answer = textBox1.Text;

            user3Answer = user3Answer.ToLower();

            if (user3Answer == null) { blackRoom(); }

            else if (user3Answer == "look sarcophagus") { sarcRoom2(); }

            else if (user3Answer == "open sarcophagus") { sarcRoom3(); }

            else if (user3Answer == "use mask") { sarcRoom9(); }

            else {
                blackRoom();
            }
        }

        private void sarcRoom3()
        {
            label1.Text = "Ancient, stale air...";
            button1.Click += new EventHandler(this.button1_ClickSarc3);
        }

        private void button1_ClickSarc3(object sender, EventArgs e)
        {
            string user3Answer = textBox1.Text;

            user3Answer = user3Answer.ToLower();

            if (user3Answer == null) { sarcRoom3(); }

            else if (user3Answer == "use sword") { sarcRoom4(); }

            else if (user3Answer == "use shield") { sarcRoom5(); }

            else if (user3Answer == "use mask") { sarcRoom9(); }

            else {
                sarcRoom3();
            }
        }





... and so on for about 1500 more lines.

What is slowing this down as it runs? Is it possible that too many "if else" statements can cause it to bog down? Should I not be trying to do this on a Windows Form Application? Should I not be testing in debug mode? Please your assistance is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Simple Windows Forms Application very slow during testing

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13962
  • View blog
  • Posts: 55,726
  • Joined: 12-June 08

Re: Simple Windows Forms Application very slow during testing

Posted 04 May 2016 - 10:20 AM

Are you running 'insights' or any 'diagnostic' tools by chance?

Why are you adding eventhandlers all over the place? At worst enable/disable buttons, but what you have there is bad practice.

Why isn't all of this encapsulated in something more portable and usable.. like classes and what not?
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,480
  • Joined: 12-December 12

Re: Simple Windows Forms Application very slow during testing

Posted 04 May 2016 - 10:31 AM

Is this ASP.NET? Reads more like a WinForms app to me.

Moved to C# forum.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Simple Windows Forms Application very slow during testing

Posted 04 May 2016 - 01:09 PM

Well, the title indicates that it's windows forms...was this posted in ASP.NET originally?
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,480
  • Joined: 12-December 12

Re: Simple Windows Forms Application very slow during testing

Posted 04 May 2016 - 01:10 PM

Yup. (I think someone might have added Windows Forms to the title subsequently.)
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6115
  • View blog
  • Posts: 21,051
  • Joined: 05-May 12

Re: Simple Windows Forms Application very slow during testing

Posted 04 May 2016 - 02:56 PM

To make matters worse, after adding an event handler, if you go through the same code path, the event handler gets added in again. So now the handler will get called multiple times.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1