lundi 17 juillet 2023

My application on Windows Forms goes into break when I try to call these functions

I have files on my computer containing all cards within a deck. What I want to do is generate all 52 numbers and give 7 of them to the user in the form of buttons so they can play these cards, another 7 cards to the computer, and the remaining cards stored in a queue for either of them to press to get more if needed. But the error I am facing is generating the 7 random numbers that need to be stored for the user to get.

This is the code I am referring to:

I believe this part of the code calls the SetButtonImage to begin to compile on load.

public FmClassic()
        {
            InitializeComponent();

            SetButtonImage();
        }

This is SetButtonImage gathering the images from within my computer as well as calling another function to generate the random numbers.

private void SetButtonImage()
        {
            button1.Image = Image.FromFile($"C:\\filepath\\{ShuffleOneTime()[0]}.jpg");
            button2.Image = Image.FromFile($"C:\\filepath\\{ShuffleOneTime()[1]}.jpg");
            button3.Image = Image.FromFile($"C:\\filepath\\{ShuffleOneTime()[2]}.jpg");
            button4.Image = Image.FromFile($"C:\\filepath\\{ShuffleOneTime()[3]}.jpg");
            button5.Image = Image.FromFile($"C:\\filepath\\{ShuffleOneTime()[4]}.jpg");
            button6.Image = Image.FromFile($"C:\\filepath\\{ShuffleOneTime()[5]}.jpg");
            button7.Image = Image.FromFile($"C:\\filepath\\{ShuffleOneTime()[6]}.jpg");

        }

This is then called to ensure they are random and not called twice with the hash table as well as converting to a string which I think is better for when searching for files?

public string ShuffleOneTime()
        {
            Random random = new Random();
            HashSet<int> generatednumbers = new HashSet<int>();
            int randomNumber = GenerateRandomNumber(random, generatednumbers);
            int numberOfUniqueNumbers = 54;
            for (int i = 0; i < numberOfUniqueNumbers; i++)
            {
                randomNumber = GenerateRandomNumber(random, generatednumbers);
                if (i == 7)
                {
                    List<int> numbers = new List<int>();
                    numbers.Add(randomNumber);
                }
            }
            string newrandomNumber = randomNumber.ToString();

            return newrandomNumber;
        }

This helps generate the numbers and return it back to the other function.

static int GenerateRandomNumber(Random random, HashSet<int> generatednumbers)
        {
            int randomNumber;
            do
            {
                randomNumber = random.Next(1, 54);
            }
            while (generatednumbers.Contains(randomNumber));

            generatednumbers.Add(randomNumber);

            return randomNumber;
        }

I have tried making it a string rather than an integer to help find it but it won't work. The filenames for the cards are all 1.jpg, 2.jpg etc so I don't know why there is an error. Everything compiles but then my screen freezes for 60 seconds and them inputs this error:

Managed Debugging Assistant 'ContextSwitchDeadlock' : 'The CLR has been unable to transition from COM context 0xa73c68 to COM context 0xa73bb0 for 60 seconds.




Aucun commentaire:

Enregistrer un commentaire