Visual Studio Development Bookmark and Share   
 index > Visual C# Express Edition > C# application hanging after MessageBox displayed
 

C# application hanging after MessageBox displayed

Hello!

I wrote a password generating app and I'm looking for some help to figure out the cause of it hanging in a certain scenario. There are four checkboxes in the form; each one of them is used to increase the complexity of password creation. I wanted to do a check to make sure at least one of the checkboxes is selected (since there is no base word library to draw from), so I wrote in some code that calls a messagebox to pop up and let the user know that at least one of the checkboxes must be selected. The app works fine if at least one checkbox is selected but if none are and the user clicks the button to create the password, the messagebox pops up and provides the message but the application hangs after that point. Can someone please point me in the right direction? Here is the area of code that the problem is occurring in:

        private void btnGenerate_Click(object sender, EventArgs e)
        {       
            txtOutput.Text = "";

            int NumberOfPasswords = int.Parse(txtNumPass.Text);
            int PasswordLength = int.Parse(txtLength.Text);

            if (
                    (cbLower.Checked == false) && (cbUpper.Checked == false) && (cdNumbers.Checked == false) && (cbSymbols.Checked == false))

                    MessageBox.Show("You must select at least one password complexity option.");

            for (int i = 0; i < NumberOfPasswords; i++)  //generate each password
            {
                for (int g = 0; g < PasswordLength; g++) //generate each character value
                {
                    txtOutput.Text += GetCharacter();
                    
                }
                txtOutput.Text += "\r\n";  //seperate the passwords to multiple lines
            }            
        }


ChocolateSoul  Tuesday, October 06, 2009 4:50 PM
Do it like this....


        private void btnGenerate_Click(object sender, EventArgs e)
        {
            txtOutput.Text = "";
            int NumberOfPasswords = int.Parse(txtNumPass.Text);
            int PasswordLength = int.Parse(txtLength.Text);
            if (
                    (cbLower.Checked == false) && (cbUpper.Checked == false) && (cdNumbers.Checked == false) && (cbSymbols.Checked == false))
            {
                MessageBox.Show("You must select at least one password complexity option.");
            }
            else
            {
                for (int i = 0; i < NumberOfPasswords; i++)  //generate each password
                {
                    for (int g = 0; g < PasswordLength; g++) //generate each character value
                    {
                        txtOutput.Text += GetCharacter();
                    }
                    txtOutput.Text += "\r\n";  //seperate the passwords to multiple lines
                }
            }
        }


And don't forget to fix your GeCharacter method.

Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Tuesday, October 06, 2009 7:31 PM
Use Debug + Break All and post the call stack.

Hans Passant.
nobugz  Tuesday, October 06, 2009 5:21 PM
Thanks for your help. Here is the call stack:

     mscorlib.dll!System.Random.Sample() + 0x6 bytes   
     mscorlib.dll!System.Random.Next(int minValue = 0, int maxValue) + 0x49 bytes   
>    Anomaly.exe!Anomaly.frmPasswordGenerator.GetCharacter() Line 58 + 0xc bytes    C#
     Anomaly.exe!Anomaly.frmPasswordGenerator.btnGenerate_Click(object sender = {Text = Cannot evaluate expression because the code of the current method is optimized.}, System.EventArgs e = {X = 18 Y = 7 Button = Left}) Line 35 + 0x22 bytes    C#
     System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs e) + 0x70 bytes   
     System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs e) + 0x4a bytes   
     System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs mevent = {X = 18 Y = 7 Button = Left}) + 0xac bytes   
     System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button, int clicks) + 0x28f bytes   
     System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) + 0x885 bytes   
     System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m) + 0x127 bytes   
     System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m) + 0x20 bytes   
     System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x10 bytes   
     System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x31 bytes   
     System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg = 514, System.IntPtr wparam, System.IntPtr lparam) + 0x57 bytes   
     [Native to Managed Transition]   
     [Managed to Native Transition]   
     System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID, int reason = -1, int pvLoopData = 0) + 0x24e bytes   
     System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = -1, System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.ApplicationContext}) + 0x177 bytes   
     System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes   
     System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes   
     Anomaly.exe!Anomaly.Program.Main() Line 17 + 0x1d bytes    C#
     [Native to Managed Transition]   
     [Managed to Native Transition]   
     mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x3a bytes   
     Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x2b bytes   
     mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x66 bytes   
     mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6f bytes   
     mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   

ChocolateSoul  Tuesday, October 06, 2009 5:53 PM
Cannot reproduce your problem from your posted code. 
The only unknown I have at this point is your GetCharacter method.
This is what I used to make it work.

private string GetCharacter()
{
    return "a";
}

I noticed some references to the Random class in your stack trace.
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Tuesday, October 06, 2009 6:26 PM
Yeah, GetCharacter is the problem.  Odds are that it is stuck in an endless loop.  I'd guess it is trying to get a random number it will never get.  Easy to debug, just start single-stepping after you break.

Hans Passant.
nobugz  Tuesday, October 06, 2009 6:57 PM
Thanks for the reply. This issue is puzzling me.

Here is the GetCharacter code:

private string GetCharacter()
        {
            
            string[] Lower = new string[26] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
            string[] Upper = new string[26] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            string[] Number = new string[10] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
            string[] Symbol = new string[] { "!", "@", "#", "$", "%", "&", "*", "?" };

            //Generate a random number

            Random rand = new System.Random(Guid.NewGuid().GetHashCode());
            int rplace = rand.Next(0,4);
            int rplace2 = 0;
            while (!IsChecked(rplace)){
                rplace = rand.Next(0, 4);
            }
            switch (rplace)
            {
                case 0:
                    rplace2 = rand.Next(0, 26);
                    return Lower[rplace2];
                case 1:
                    rplace2 = rand.Next(0, 26);
                    return Upper[rplace2];
                case 2:
                    rplace2 = rand.Next(0, 10);
                    return Number[rplace2];
                case 3:
                    rplace2 = rand.Next(0, 8);
                    return Symbol[rplace2];
                default:
                    return "";
            }
        }

ChocolateSoul  Tuesday, October 06, 2009 7:01 PM
You have this in your GetCharacter method:

 while (!IsChecked(rplace)){
                rplace = rand.Next(0, 4);
            }

If none of your check boxes are checked, that will loop forever.  You need to put in special handling for that case.
Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  Tuesday, October 06, 2009 7:06 PM
Just put curly braces around the code after the if-statement.  Your message box says you can't generate a password, but then you do anyway.

Hans Passant.
nobugz  Tuesday, October 06, 2009 7:11 PM
I tried putting the curly braces around the rest of the code in that block but that resulted in the same issue. Can you provide further assistance? I really appreciate the help from everyone, as I'm fairly new to C# and I'm trying to figure out where I'm going wrong. :)
ChocolateSoul  Tuesday, October 06, 2009 7:28 PM
Do it like this....


        private void btnGenerate_Click(object sender, EventArgs e)
        {
            txtOutput.Text = "";
            int NumberOfPasswords = int.Parse(txtNumPass.Text);
            int PasswordLength = int.Parse(txtLength.Text);
            if (
                    (cbLower.Checked == false) && (cbUpper.Checked == false) && (cdNumbers.Checked == false) && (cbSymbols.Checked == false))
            {
                MessageBox.Show("You must select at least one password complexity option.");
            }
            else
            {
                for (int i = 0; i < NumberOfPasswords; i++)  //generate each password
                {
                    for (int g = 0; g < PasswordLength; g++) //generate each character value
                    {
                        txtOutput.Text += GetCharacter();
                    }
                    txtOutput.Text += "\r\n";  //seperate the passwords to multiple lines
                }
            }
        }


And don't forget to fix your GeCharacter method.

Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Tuesday, October 06, 2009 7:31 PM
That change worked great! Thanks so much for your help! As for the GetCharacter code getting fixed, I have a boolean value named IsChecked that is part of a routine that checks to see if a random value matches with any of the checkboxes. Is that sufficient or do you suggest something different?
ChocolateSoul  Tuesday, October 06, 2009 7:42 PM
Hi,

It appears this would work smoothly.

Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Harry Zhu  Monday, October 12, 2009 3:20 AM

You can use google to search for other answers

Custom Search

More Threads

• Dates in SQL string
• C# Math Problem
• how to display data on 2 data grid view at the same time using c sharp
• Can´t see web application when New Project
• Problem with bound Checkboxes
• Data Sources window doesn't display sources
• saving textbox values to text file
• How to Detect another applications GUI controls in C#
• Scaling the form
• Visual C# baisic screen saver freezes