How to wait for Screen.SetResolution to finish?

Because of technical concerns, I need to run my Unity application in a “fullscreen” borderless windowed mode. The Unity window position and size is red from an external configuration file on start.

Screen.fullScreen = false;
Application.runInBackground = true;
Screen.SetResolution (windowWidth, windowHeight, false);
// ** Need to wait for the resolution to be changed here **
SetPosition(windowX, windowY);

As you may be aware of, the Screen.SetResolution function takes a few seconds to get its job done. It runs on another thread. The problem with this code is that the window position can’t be changed while the resolution is changing.
Is there a way to ‘block’ the function until the resolution has changed?

I was thinking of a line like:

WaitUntil (Screen.GetResolution() == wantedResolution)

Note: The SetPosition() function here calls SetWindowPos from user32.dll

Edit: This is written in C#!

I found a quick fix for this issue.

I set my default settings in (File > Build Settings > Player Settings) which makes the code runs faster.

Then, I changed the code for:

Screen.SetResolution (windowWidth, windowHeight, false);
yield return new WaitForSeconds(0.5F);
SetPosition(windowX, windowY);

This works properly; however a cleaner solution would be appreciated.

yield Screen.SetResolution (windowWidth, windowHeight, false);

Here’s a cleaner fix than waiting for 0.5f seconds.

You can use a coroutine with WaitUntil()

Like so:

void ApplyResolution(Resolution resolution) {
    Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen, targetFPS);

IEnumerator _CalculateScreenResolution(Resolution targetResolution) {
    yield return new WaitUntil(() => Screen.width == targetResolution.width 
        && Screen.height == targetResolution.height);