Question Password Generator Class -invalid

tklustig

New member
Joined
Mar 5, 2017
Messages
1
Programming Experience
1-3
Hi guys,
beforehand I'll post the code of a class which should generate a password!
C#:
using System.Collections.Generic;
using System.Threading;


namespace Input_Output
{
    public class PasswordGenerator
    {
        Queue<string> Results; // Warteschlange mit erzeugten Passw?rtern


        bool UpperAZ; // true wenn Gro?buchstaben benutzt werden sollen
        bool LowerAZ; // true wenn Kleinbuchstaben benutzt werden sollen
        bool Numbers; // true wenn Zahlen benutzt werden sollen


        int Min; // minimale L?nge des Passworts
        int Max; // maximale L?nge des Passworts


        const int MaxQueueSize = 30000000; // maximale Zahl von Passw?rtern, die gleichzeitig in der Queue gespeichert werden d?rfen (Schutz vor Speicher?berlauf)


        public PasswordGenerator(bool upperAZ, bool lowerAZ, bool numbers, int min, int max)
        {
            UpperAZ = upperAZ;
            LowerAZ = lowerAZ;
            Numbers = numbers;


            Min = min;
            Max = max;
            Results = new Queue<string>();


            // neuen Thread zur Passwortgenerierung erstellen und starten
            Thread Creator = new Thread(new ThreadStart(FillQueue));
            Creator.Start();
        }


        private void FillQueue()
        {
            // Warteschlange f?llen
            for (int i = Min; i <= Max; i++)
            {
                SetLetter(i, 0, "");
            }
        }


        private void SetLetter(int length, int pos, string temp)
        {
            // aktuelle Position mit allen m?glichen Zeichen f?llen
            if (UpperAZ)
            {
                for (int i = 65; i <= 90; i++)
                {
                    NextStep(length, pos, temp + (char)i);
                }
            }
            if (LowerAZ)
            {
                for (int i = 97; i <= 122; i++)
                {
                    NextStep(length, pos, temp + (char)i);
                }
            }
            if (Numbers)
            {
                for (int i = 0; i <= 9; i++)
                {
                    NextStep(length, pos, temp + i.ToString());
                }
            }
        }


        private void NextStep(int length, int pos, string temp)
        {
            // Funktion zum Abschlie?en eines Schrittes


            // ist die Warteschlange "voll", wird der Thread pausiert
            while (Results.Count > MaxQueueSize)
                Thread.Sleep(500);


            // ist noch nicht die Endposition im Passwort erreicht, wird SetLetters() mit der n?chsten Position aufgerufen
            if (pos < length - 1)
                SetLetter(length, pos + 1, temp);
            else
                // ansonsten wird das aktuelle Passwort der Warteschlange hinzugef?gt
                Results.Enqueue(temp);
        }


        public string Dequeue()
        {
            // liefert das unterste Element der Warteschlange
            
                return Results.Dequeue();
      
              
        }
    }
}

Unfortunately, if I try to generate one and to put it out in a Textbox, I get the error:
InvalidOperationException: Waiting queue is empty!"
Where is the logical mistake.There seems to be no syntax error!

C#:
using System.Windows;






namespace Input_Output
{
    /// <summary>
    /// Interaktionslogik f?r MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
     


        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            //Klasse: MessageBox
            //namespace:System.Windows
            //Methoden:Equals,ReferenceEquals,Show
            //Enummerationen der Methode Show(u.a.):MessageBoxButton,MessageBoxImage)
            MessageBoxResult result = MessageBox.Show("Wirklich beenden?", "Stop?", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
            {
                Application.Current.Shutdown();
            }
        }


        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (this.txt0.Text == "")
            {
               this.txt0.Text="Bitte Feld ausf?llen";
            }
         
            else
            {
                this.txt2.Text = this.txt0.Text;
                PasswordGenerator Generator = new PasswordGenerator(true, true, true, 1, 7);
                string passwort=Generator.Dequeue();
                this.txt3.Text = passwort;


            }
            
        }
    }
 
You have a Queue<string> and you have an error message telling you that a queue is empty. I think the issue should be fairly clear: your Queue<string> is empty when you try to dequeue. To find out more, you need to debug your code. Watch the queue as you debug and make sure that it is being populated as you expect. You must be either making a mistake putting items in or a mistake taking them out. Debugging will enable you to determine which.
 
Your constructor starts a thread, but caller doesn't wait for this. You should remove the threading from constructor and make FillQueue a public async method that caller can await.
 
Back
Top Bottom