11 Replies - 4136 Views - Last Post: 29 November 2010 - 03:24 PM Rate Topic: -----

#1 jemsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 11-November 10

Program hangs immediately when debugging from Visual Studio.

Posted 29 November 2010 - 02:01 PM

I have a program to read data via user input from a form and then it stores it to a file.

However when i try debug my program, it says "the program has stopped working" then it give the user the usual options "find a solution online to fix the problem", "close to program" or "debud the program".

Ive tried to debug then it comes to a problem in the generated code which is:
Application.Run(new WriteFriendRecords());



anyone have any ideas? bit annoying pretty sure the code I wrote works and I just want to test it! ^-^

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Program hangs immediately when debugging from Visual Studio.

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5676
  • View blog
  • Posts: 12,194
  • Joined: 02-June 10

Re: Program hangs immediately when debugging from Visual Studio.

Posted 29 November 2010 - 02:10 PM

We would need to see the code.
But it looks like you are calling a method rather than opening a form.

If "WriteFriendRecords" is a form name, then we need to see the constructor and on_load code to figure out where it is going wrong.

But you should be able to put a breakpoint at the first line of the constructor then walk through exectution one line at a time until it hangs up. That's basic debugging 101.


Debugging tutorial
Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland  Icon User is offline

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


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Program hangs immediately when debugging from Visual Studio.

Posted 29 November 2010 - 02:10 PM

Well, that's the code that starts your initial form. Is there no other information associated with your exception? You are running this in Visual Studio from the debugger, correct? It usually doesn't give you such a lean error message.
Was This Post Helpful? 1
  • +
  • -

#4 jemsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 11-November 10

Re: Program hangs immediately when debugging from Visual Studio.

Posted 29 November 2010 - 02:24 PM

yip running in visual studio and yes im finding it werid that it is not showing me any other type of error.

ok il show all my code. this is the code i have wrote by double clicking on my form called WriteFriendRecords.cs:

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

namespace WriteFriendRecords.cs
{
    public partial class WriteFriendRecords : Form
    {
        
            const string DELIM = ",";
            const string FILENAME = @"C:\Users\Jemma\Documents\Visual Studio 2008\Projects\WriteFriendRecords.cs\WriteFriendRecords.cs";
            
            string Fname, Sname, Birth;
            int num;

            static FileStream outFile = new FileStream(FILENAME, FileMode.Create,
                FileAccess.Write);
            StreamWriter Writer = new StreamWriter(outFile);

        public WriteFriendRecords()
        {
            InitializeComponent();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            Fname = txtFname.Text;
            Sname = txtSname.Text;
            Birth = txtBirth.Text;
            num = Convert.ToInt32(txtPNo.Text);
            Writer.WriteLine (Fname + DELIM + Sname + DELIM + Birth + DELIM + num);
            txtFname.Clear();
            txtSname.Clear();
            txtPNo.Clear();
            txtBirth.Clear();
        }

         
         }

    }






This is my code for the dispose method called WriteFriendRecordsDesigner.cs:

namespace WriteFriendRecords.cs
{
    partial class WriteFriendRecords
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            Writer.Close();
            outFile.Close();
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lblFname = new System.Windows.Forms.Label();
            this.lblSname = new System.Windows.Forms.Label();
            this.lblPNo = new System.Windows.Forms.Label();
            this.lblBirth = new System.Windows.Forms.Label();
            this.txtFname = new System.Windows.Forms.TextBox();
            this.txtSname = new System.Windows.Forms.TextBox();
            this.txtPNo = new System.Windows.Forms.TextBox();
            this.txtBirth = new System.Windows.Forms.TextBox();
            this.lblHeader = new System.Windows.Forms.Label();
            this.btnEnter = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // lblFname
            // 
            this.lblFname.AutoSize = true;
            this.lblFname.Location = new System.Drawing.Point(53, 71);
            this.lblFname.Name = "lblFname";
            this.lblFname.Size = new System.Drawing.Size(57, 13);
            this.lblFname.TabIndex = 0;
            this.lblFname.Text = "Forename:";
            // 
            // lblSname
            // 
            this.lblSname.AutoSize = true;
            this.lblSname.Location = new System.Drawing.Point(53, 102);
            this.lblSname.Name = "lblSname";
            this.lblSname.Size = new System.Drawing.Size(52, 13);
            this.lblSname.TabIndex = 1;
            this.lblSname.Text = "Surname:";
            // 
            // lblPNo
            // 
            this.lblPNo.AutoSize = true;
            this.lblPNo.Location = new System.Drawing.Point(53, 137);
            this.lblPNo.Name = "lblPNo";
            this.lblPNo.Size = new System.Drawing.Size(79, 13);
            this.lblPNo.TabIndex = 2;
            this.lblPNo.Text = "Phone number:";
            // 
            // lblBirth
            // 
            this.lblBirth.AutoSize = true;
            this.lblBirth.Location = new System.Drawing.Point(53, 170);
            this.lblBirth.Name = "lblBirth";
            this.lblBirth.Size = new System.Drawing.Size(115, 13);
            this.lblBirth.TabIndex = 3;
            this.lblBirth.Text = "Day and month of Birth";
            // 
            // txtFname
            // 
            this.txtFname.Location = new System.Drawing.Point(238, 73);
            this.txtFname.Name = "txtFname";
            this.txtFname.Size = new System.Drawing.Size(100, 20);
            this.txtFname.TabIndex = 1;
            // 
            // txtSname
            // 
            this.txtSname.Location = new System.Drawing.Point(238, 99);
            this.txtSname.Name = "txtSname";
            this.txtSname.Size = new System.Drawing.Size(100, 20);
            this.txtSname.TabIndex = 2;
            // 
            // txtPNo
            // 
            this.txtPNo.Location = new System.Drawing.Point(238, 134);
            this.txtPNo.Name = "txtPNo";
            this.txtPNo.Size = new System.Drawing.Size(100, 20);
            this.txtPNo.TabIndex = 3;
            // 
            // txtBirth
            // 
            this.txtBirth.Location = new System.Drawing.Point(238, 163);
            this.txtBirth.Name = "txtBirth";
            this.txtBirth.Size = new System.Drawing.Size(100, 20);
            this.txtBirth.TabIndex = 4;
            // 
            // lblHeader
            // 
            this.lblHeader.AutoSize = true;
            this.lblHeader.Location = new System.Drawing.Point(53, 20);
            this.lblHeader.Name = "lblHeader";
            this.lblHeader.Size = new System.Drawing.Size(312, 13);
            this.lblHeader.TabIndex = 8;
            this.lblHeader.Text = "Please enter 10 of your friends data in the boxes provided below:";
            // 
            // btnEnter
            // 
            this.btnEnter.Location = new System.Drawing.Point(488, 308);
            this.btnEnter.Name = "btnEnter";
            this.btnEnter.Size = new System.Drawing.Size(75, 23);
            this.btnEnter.TabIndex = 9;
            this.btnEnter.Text = "Enter Record";
            this.btnEnter.UseVisualStyleBackColor = true;
            this.btnEnter.Click += new System.EventHandler(this.btnEnter_Click);
            // 
            // FrmFriendData
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(612, 347);
            this.Controls.Add(this.btnEnter);
            this.Controls.Add(this.lblHeader);
            this.Controls.Add(this.txtBirth);
            this.Controls.Add(this.txtPNo);
            this.Controls.Add(this.txtSname);
            this.Controls.Add(this.txtFname);
            this.Controls.Add(this.lblBirth);
            this.Controls.Add(this.lblPNo);
            this.Controls.Add(this.lblSname);
            this.Controls.Add(this.lblFname);
            this.Name = "FrmFriendData";
            this.Text = "Friends Data";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lblFname;
        private System.Windows.Forms.Label lblSname;
        private System.Windows.Forms.Label lblPNo;
        private System.Windows.Forms.Label lblBirth;
        private System.Windows.Forms.TextBox txtFname;
        private System.Windows.Forms.TextBox txtSname;
        private System.Windows.Forms.TextBox txtPNo;
        private System.Windows.Forms.TextBox txtBirth;
        private System.Windows.Forms.Label lblHeader;
        private System.Windows.Forms.Button btnEnter;
    }
}





This is the code that opens when i debug and break called program.cs:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WriteFriendRecords.cs
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new WriteFriendRecords());
        }
    }
}




THANKS I will be intending to delete the code i have wrote in this post as it is for a queation for an assignment.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5676
  • View blog
  • Posts: 12,194
  • Joined: 02-June 10

Re: Program hangs immediately when debugging from Visual Studio.

Posted 29 November 2010 - 02:38 PM

What the frak?
const string FILENAME = @"C:\Users\Jemma\Documents\Visual Studio 2008\Projects\WriteFriendRecords.cs\WriteFriendRecords.cs";
static FileStream outFile = new FileStream(FILENAME, FileMode.Create, FileAccess.Write);



You're trying to write text into your Form's C-Sharp code file? No wonder it breaks.


Writing a text file tutorial.
Reading a text file tutorial.
Was This Post Helpful? 3
  • +
  • -

#6 Curtis Rutland  Icon User is offline

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


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Program hangs immediately when debugging from Visual Studio.

Posted 29 November 2010 - 02:45 PM

Interestingly enough, when I copy and paste all your code into my own project, it starts and runs. The only thing I had to change was the constant FILENAME to a folder that existed.

My suggestion to you though, is to make the FileStream non-static. There's no good reason for it to be.

Static fields are initialized the first time the class is referenced. So the exception is happening before the constructor is being called. Try removing the keyword "static" and seeing what happens. To do this, you'll need to move initialization into the constructor:

FileStream outFile;
StreamWriter Writer;

public WriteFriendRecords()
{
    InitializeComponent();
    outFile = new FileStream(FILENAME, FileMode.Create, FileAccess.Write);
    Writer = new StreamWriter(outFile);
}


EDIT: Didn't even notice the issue tlhintoq pointed out. In my skimming, I assumed it was just a directory that existed in my system. That, in addition to what I said, is going to be an issue. Visual Studio locks the files for editing when the program is running, and your program tries to obtain a Write lock on one of the locked files. That call will fail, and the program will fail.

What I mentioned is actually an issue, but I do believe that tlhintoq found the exact reason for the crashing.

This post has been edited by insertAlias: 29 November 2010 - 02:49 PM

Was This Post Helpful? 2
  • +
  • -

#7 jemsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 11-November 10

Re: Program hangs immediately when debugging from Visual Studio.

Posted 29 November 2010 - 02:47 PM

ok that is true suppose im not that sure what im doing.

what should i be writing to create that file?

thanks
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

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


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Program hangs immediately when debugging from Visual Studio.

Posted 29 November 2010 - 02:52 PM

Choose a path to a text file somewhere on your drive. I like to make a directory called "c:\dev\" and put all my testing text files there. But you could use Application.StartupPath or you could use this:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)


To get a path to the AppData folder. From there, you can create a new text file. Just don't try to edit your code files via code.
Was This Post Helpful? 1
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5676
  • View blog
  • Posts: 12,194
  • Joined: 02-June 10

Re: Program hangs immediately when debugging from Visual Studio.

Posted 29 November 2010 - 02:56 PM

Quote

what should i be writing to create that file?

Do the two tutorials that were giving to you already on reading and writing text files, rather than ask people to repeat the information that is already out there.
Was This Post Helpful? 0
  • +
  • -

#10 jemsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 11-November 10

Re: Program hangs immediately when debugging from Visual Studio.

Posted 29 November 2010 - 03:09 PM

At the start of making this program I was wondering should i be going to notepad an saving a file called for example data.txt?

then where I save this empty folder is the path I should be writing for

const string FILENAME = @"U/documents/Data.txt";



see I have to put this program onto a disc and it has to work for my teacher. I have changed my code to you post before the last and have tryed your last suggestions but its not working.

thanks
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is offline

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


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Program hangs immediately when debugging from Visual Studio.

Posted 29 November 2010 - 03:15 PM

First, I'd ask your teacher what path he intended you to put the file at. He should have an answer for you. If he doesn't, I'd try putting the file at either the Application.StartupPath or the AppData folder as I mentioned earlier.

If you have to use these, then you can't use your constant in the form called FILENAME. These paths I'm mentioning already come from constants. Instead of using FILENAME, just use whichever one of the ones I've mentioned suits you. You'd have to change it to something like this:

string filename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
outFile = new FileStream(filename, FileMode.Create, FileAccess.Write);



Also note that you don't have to create the file first, since you're using FileMode.Create in your FileStream.

One more thing:

Quote

but its not working


That's never enough information. If you're getting errors, we need to know exactly what they are, what line is throwing them, and if they have any InnerExceptions. If you're not getting errors, you need to explain what the expected behavior is, and what the actual behavior is.

This post has been edited by insertAlias: 29 November 2010 - 03:18 PM

Was This Post Helpful? 1
  • +
  • -

#12 jemsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 11-November 10

Re: Program hangs immediately when debugging from Visual Studio.

Posted 29 November 2010 - 03:24 PM

Ok Ive got it working thanks for the help was just my file.

thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1