# Closing window without waiting for AsychFlip

**URL:** <https://psychtoolbox.discourse.group/t/closing-window-without-waiting-for-asychflip/3409>\
**Category:** Programming Help\
**Created:** [September 20, 2020, 3:59pm UTC](https://psychtoolbox.discourse.group/t/closing-window-without-waiting-for-asychflip/3409 "2020-09-20T15:59:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Max](https://psychtoolbox.discourse.group/u/Max)\
**Post date:** [September 20, 2020, 3:59pm UTC](https://psychtoolbox.discourse.group/t/closing-window-without-waiting-for-asychflip/3409/1 "2020-09-20T15:59:15Z")

</div>

Hi everyone,

I’ve setup a Psychtoolbox experiment that uses a main loop to check for specific (3rd party hardware) input while calls to `AsyncFlipBegin` and `AsyncFlipCheckEnd` are being made to control what is drawn on the screen.

All the way at the end of that loop, the following code checks whether the escape-key is pressed:

```auto
[~, ~, code] = KbCheck(-1);
if code(27)
    running = 0;
    break;
end

```

Pressing esc immediately breaks the loop and should end the program.  
Straight after the loop `sca()` is called. Exiting the loop works perfectly, I’ve checked with console output.

However, the problem is that it (`sca`) waits for asynchronous flips to finish (`AsyncFlipEnd` is in there), causing a 10 to 20-second delay for the window to close. I had no luck finding a psychtoolbox function to interrupt the `AsyncFlipBegin` call, nor could I find a solution on this forum.

Interrupting such an asynchronous event or closing down the Psychtoolbox/GL loop before a planned event seems pretty straightforward, so I probably overlooked something very simple.  
Would you be able to point me in the right direction?

Thanks in advance

Max

---

<div class="post-metadata">

**Author:** ![xiangrui](https://avatars.discourse-cdn.com/v4/letter/x/ec9cab/32.png) [@xiangrui](https://psychtoolbox.discourse.group/u/xiangrui)\
**Post date:** [September 20, 2020, 6:32pm UTC](https://psychtoolbox.discourse.group/t/closing-window-without-waiting-for-asychflip/3409/2 "2020-09-20T18:32:03Z")

</div>

> [@Max](#):
>
> `AsyncFlipBegin`

Would it help to do `AsyncFlipEnd` right before you `break` out after ESC is pressed?

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [September 21, 2020, 1:55pm UTC](https://psychtoolbox.discourse.group/t/closing-window-without-waiting-for-asychflip/3409/3 "2020-09-21T13:55:29Z")

</div>

That’s intentional and necessary that you have to wait for async flips to complete before closing the window.

You could call Screen(‘CloseAll’) before sca; to force a shutdown, but that will print some angry warnings and is not recommended, as in some cases it can cause Matlab/Octave to hang or crash.

-mario

---

<div class="post-metadata">

**Author:** ![Max](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Max](https://psychtoolbox.discourse.group/u/Max)\
**Post date:** [September 21, 2020, 5:02pm UTC](https://psychtoolbox.discourse.group/t/closing-window-without-waiting-for-asychflip/3409/4 "2020-09-21T17:02:52Z")

</div>

> [@xiangrui](#):
>
> `AsyncFlipEnd`

I don’t think so, calling `AsyncFlipEnd` would only shift the wait (that now happens in `sca`) forward, but not interrupt the wait.

> [@mariokleiner](#):
>
> That’s intentional and necessary that you have to wait for async flips to complete before closing the window.
> 
> You could call Screen(‘CloseAll’) before sca; to force a shutdown, but that will print some angry warnings and is not recommended, as in some cases it can cause Matlab/Octave to hang or crash.
> 
> -mario

I see, so there is no way to interrupt a pending async flip.  
Too bad, I guess I will just use Screen(‘CloseAll’) and cross my fingers that matlab survives 🙂

Thank you!
