UI latency and panel loading issues

I have a project that encrypts a given file, you have to fill in the required details and then a screen should pop up that displays the percentage of file encrypted. The issue is when I click the encrypt button everything freezes the screen pops up after the encryption is complete showing the encryption complete message this is noticeable when I try to encrypt larger files.

In my code I first activate the panel and then I have called the function that encrypts the file, still it freezes until the encryption is complete and then the “encryption complete” message on the panel pops up.

ciphDescPanel.SetActive(false);
encryptionPanel.SetActive(true); //this panel should show when "encrypt" button is clicked, but it shows when the functions(cipherCallBacks.__CIPHER() ) called below complete their execution.
string empty = "_EMPTY";

if (encryptionPanel.activeInHierarchy == true) {
		if (passwordPin.isOn)
			cipherCallBacks.__CIPHER (filePathString, cipherFileDirString, paPiEntryString, empty, keyLength); //function to encrypt file
		else if (key.isOn)
			cipherCallBacks.__CIPHER (filePathString, cipherFileDirString, empty, keyFilePathString, keyLength); //function to encrypt file

Well, that is why C# is serial programming and not parallel. Imagine the code as a road. A car follows a car and so on. If a car stops, all cars behind it must stop to and wait until the car continues. It is the same thing but we have to deal with data.

What you need to do, is try using a coroutine. Coroutine is like adding a parallel road for more cars to pass the same time with others.

Consider the following example :

public void DoSomeLargeMaths()
    {
        Debug.Log("Started calcualting. This may take some time. Buhaha...");
        for (int foo = 0; foo < 99999999; foo++)
        {
            float x = Mathf.Pow(2f, 5f);
            float y = Mathf.Pow(3f, 9f);
            float z = x + y;
        }
        Debug.Log("Finished. Are you still alive?");
    }

This calculation will take soooo much time to finish and all other operations after that will halt until this calculation is finished. Now, consider this :

public IEnumerator DoSomeLargeMaths()
    {
        Debug.Log("Started calcualting. This may take some time. Buhaha...");
        for (int foo = 0; foo < 99999999; foo++)
        {
            float x = Mathf.Pow(2f, 5f);
            float y = Mathf.Pow(3f, 9f);
            float z = x + y;

            yield return null;
        }
        Debug.Log("Finished. Was that long?");
    }

This function will do a calculation and halt every frame so it does not halts others operations. This will keep calculating simultaneously with other operations (showing progress etc.).

Remember that this function is called like that :

StartCoroutine(DoSomeLargeMaths());

Calling it as a simple function will bring the same results as not using coroutines.

I hope I helped.

  • yield return null orders programming to wait for a frame and the return there.