Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Visual C# 2010 - Application Crashes At A Certain Time (Causing VS2010 To Crash As Well)

Visual C# 2010 - Application Crashes At A Certain Time (Causing VS2010 To Crash As Well)

Scheduled Pinned Locked Moved C#
csharpgame-devhelplounge
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Ben Magee

    Hi, I'm making a Bingo Generator for a assignment, but it generates the last number, then should you click the button again it crashes, also crashing VS2010 so I don't get a error either. here's the button code:

    var lineCount = 1;
    using (var reader = File.OpenText(@"saved.bingo"))
    {
    while (reader.ReadLine() != null)
    {
    lineCount++;
    }
    }

                TextWriter saveCalled = new StreamWriter("saved.bingo");
                saveCalled.Write(called.Text);
                saveCalled.Close();
    
                TextReader loadSaved = new StreamReader("saved.bingo");
                string saved = loadSaved.ReadToEnd();
                loadSaved.Close();
    
                randomGenerated.generate();
    
                TextReader nextBingo = new StreamReader("bingoNext.bingo");
                string bingoNum = nextBingo.ReadToEnd();
                nextBingo.Close();
    
                bingoNum = bingoNum.Trim();
    
                if (saved.Contains(bingoNum) == true)
                {
                    randomGenerated.generate();
                }
                else
                {
                    TextReader tr = new StreamReader("saved.bingo");
                    string saveNm = tr.ReadToEnd();
                    tr.Close();
    
                    int saveNum = saveNm.Count();
    
                    if (saveNum >= 15)
                    {
                        button1.Visible = false;
                        MessageBox.Show("Game Over.");
                    }
    
                    newnum.Text = bingoNum;
    
                    TextReader nextCalled = new StreamReader("calledNext.bingo");
                    string calledNum = nextCalled.ReadToEnd();
                    nextCalled.Close();
    
                    called.Text = calledNum;
                }
    

    and the generator code:

    namespace openBingo
    {
    public class randomGenerated
    {
    public static void generate()
    {
    System.Random linegrab = new System.Random(DateTime.Now.Millisecond);

            TextReader numRead = new StreamReader("numbers.bingo");
    
            ArrayList lines = new ArrayList();
    
            string line;
    
            while ((line = numRead.ReadLine()) != null)
            {
                lines.Add(line);
            }
    
            int randomIndex = linegrab.Next(lines.Count);
    
    
            string bingoNum = (lines
    
    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #3

    Hi, you need to figure out where things go wrong. Start by using a try-catch block, and displaying the exception, all of it. Hence use something along these lines:

    try {
    // existing code goes here
    } catch (Exception exc) {
    Console.WriteLine(exc.ToString());
    }

    You can do this in the button click handler, in the generate() method, etc. Then watch the line numbers (and tell Visual to always show them, see here[^]) BTW: your program looks terrible. Many things seem less than good. - Why are you writing and reading files all the time? Don't you trust the computer's memory? FYI: there are File methods that read or write all text of a file. - For random numbers, you should only have a single RNG and call Next on it, instead of creating new ones. - generate() is recursive. Any idea what keeps it from recursing forever? (which would cause a StackOverflowException). - and please stop abusing the var keyword, use real types instead. :)

    Luc Pattyn


    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


    B 1 Reply Last reply
    0
    • L Luc Pattyn

      Hi, you need to figure out where things go wrong. Start by using a try-catch block, and displaying the exception, all of it. Hence use something along these lines:

      try {
      // existing code goes here
      } catch (Exception exc) {
      Console.WriteLine(exc.ToString());
      }

      You can do this in the button click handler, in the generate() method, etc. Then watch the line numbers (and tell Visual to always show them, see here[^]) BTW: your program looks terrible. Many things seem less than good. - Why are you writing and reading files all the time? Don't you trust the computer's memory? FYI: there are File methods that read or write all text of a file. - For random numbers, you should only have a single RNG and call Next on it, instead of creating new ones. - generate() is recursive. Any idea what keeps it from recursing forever? (which would cause a StackOverflowException). - and please stop abusing the var keyword, use real types instead. :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      B Offline
      B Offline
      Ben Magee
      wrote on last edited by
      #4

      Hi again, How would I store something in the computers memory? Ben.

      L 1 Reply Last reply
      0
      • B Ben Magee

        Hi again, How would I store something in the computers memory? Ben.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #5

        Normally you keep the data in variables as long as your app is running; and you save it to disk, if needed, when the app exits. :)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        B 1 Reply Last reply
        0
        • L Luc Pattyn

          Normally you keep the data in variables as long as your app is running; and you save it to disk, if needed, when the app exits. :)

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          B Offline
          B Offline
          Ben Magee
          wrote on last edited by
          #6

          Haha, I do try, but sometimes i need to use the data of the variable in a diffrent button control or something, and it wont let it. I tried settting it to something like: public void button1 but no luck :( Thanks, Ben.

          L 1 Reply Last reply
          0
          • B Ben Magee

            Haha, I do try, but sometimes i need to use the data of the variable in a diffrent button control or something, and it wont let it. I tried settting it to something like: public void button1 but no luck :( Thanks, Ben.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            Ben Magee wrote:

            I tried settting it to something like: public void button1 but no luckFrown

            I think maybe you need to study the basics of C#.

            B 1 Reply Last reply
            0
            • B Ben Magee

              Hi, I'm making a Bingo Generator for a assignment, but it generates the last number, then should you click the button again it crashes, also crashing VS2010 so I don't get a error either. here's the button code:

              var lineCount = 1;
              using (var reader = File.OpenText(@"saved.bingo"))
              {
              while (reader.ReadLine() != null)
              {
              lineCount++;
              }
              }

                          TextWriter saveCalled = new StreamWriter("saved.bingo");
                          saveCalled.Write(called.Text);
                          saveCalled.Close();
              
                          TextReader loadSaved = new StreamReader("saved.bingo");
                          string saved = loadSaved.ReadToEnd();
                          loadSaved.Close();
              
                          randomGenerated.generate();
              
                          TextReader nextBingo = new StreamReader("bingoNext.bingo");
                          string bingoNum = nextBingo.ReadToEnd();
                          nextBingo.Close();
              
                          bingoNum = bingoNum.Trim();
              
                          if (saved.Contains(bingoNum) == true)
                          {
                              randomGenerated.generate();
                          }
                          else
                          {
                              TextReader tr = new StreamReader("saved.bingo");
                              string saveNm = tr.ReadToEnd();
                              tr.Close();
              
                              int saveNum = saveNm.Count();
              
                              if (saveNum >= 15)
                              {
                                  button1.Visible = false;
                                  MessageBox.Show("Game Over.");
                              }
              
                              newnum.Text = bingoNum;
              
                              TextReader nextCalled = new StreamReader("calledNext.bingo");
                              string calledNum = nextCalled.ReadToEnd();
                              nextCalled.Close();
              
                              called.Text = calledNum;
                          }
              

              and the generator code:

              namespace openBingo
              {
              public class randomGenerated
              {
              public static void generate()
              {
              System.Random linegrab = new System.Random(DateTime.Now.Millisecond);

                      TextReader numRead = new StreamReader("numbers.bingo");
              
                      ArrayList lines = new ArrayList();
              
                      string line;
              
                      while ((line = numRead.ReadLine()) != null)
                      {
                          lines.Add(line);
                      }
              
                      int randomIndex = linegrab.Next(lines.Count);
              
              
                      string bingoNum = (lines
              
              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #8

              Ben Magee wrote:

              it generates the last number

              Do you mean it goes through all the available numbers and then you try to get one more? When there aren't any more? Don't you want to reset at some point?

              B 1 Reply Last reply
              0
              • P PIEBALDconsult

                Ben Magee wrote:

                it generates the last number

                Do you mean it goes through all the available numbers and then you try to get one more? When there aren't any more? Don't you want to reset at some point?

                B Offline
                B Offline
                Ben Magee
                wrote on last edited by
                #9

                When the form loads, it generates all the numbers, and writes them to a file. When you press the button, it gets a random line from that file, and then the number on it, it then checks if the number is in a file with the numbers already called, if it isnt, it adds it to that file and then displays it. Forgive me, I am still relatively new to C# - and I am looking up on sharing variables between classes now. BtM.

                P 1 Reply Last reply
                0
                • L Lost User

                  Ben Magee wrote:

                  I tried settting it to something like: public void button1 but no luckFrown

                  I think maybe you need to study the basics of C#.

                  B Offline
                  B Offline
                  Ben Magee
                  wrote on last edited by
                  #10

                  Richard MacCutchan wrote:

                  I think maybe you need to study the basics of C#.

                  Did some studying, I now understand I need a public class with static variables inside to store data. Thanks for the advice (: Ben.

                  L 1 Reply Last reply
                  0
                  • B Ben Magee

                    Richard MacCutchan wrote:

                    I think maybe you need to study the basics of C#.

                    Did some studying, I now understand I need a public class with static variables inside to store data. Thanks for the advice (: Ben.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    Ben Magee wrote:

                    I now understand I need a public class with static variables inside to store data.

                    I think my previous statement still stands. This is not the way to store data within classes or methods.

                    1 Reply Last reply
                    0
                    • B Ben Magee

                      When the form loads, it generates all the numbers, and writes them to a file. When you press the button, it gets a random line from that file, and then the number on it, it then checks if the number is in a file with the numbers already called, if it isnt, it adds it to that file and then displays it. Forgive me, I am still relatively new to C# - and I am looking up on sharing variables between classes now. BtM.

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #12

                      Regardless of the implementation; once you've selected all the numbers, there are no more to select no matter how hard you try. You need to reset.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups