# Why does this code hang ?

**URL:** <https://psychtoolbox.discourse.group/t/why-does-this-code-hang/468>\
**Category:** Uncategorized\
**Created:** [November 21, 2011, 7:16pm UTC](https://psychtoolbox.discourse.group/t/why-does-this-code-hang/468 "2011-11-21T19:16:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [November 21, 2011, 7:16pm UTC](https://psychtoolbox.discourse.group/t/why-does-this-code-hang/468/1 "2011-11-21T19:16:52Z")

</div>

Hi everyone,  
  
When I run the following code ;  
  
%%%%%%  
screen=0;  
[wPtr,rect]=Screen('OpenWindow',screen, [], []);  
HideCursor;  
tic  
freq\_Hz = 20 ;  
secs = 1 / freq\_Hz;  
measurement\_time = 5;  
i=1;  
t0 = clock;  
pwm = zeros(1, 100000);  
while etime(clock, t0) \< measurement\_time  
disp(clock);  
while etime(clock, t0) \< secs  
disp(clock);  
black=BlackIndex(wPtr);  
pwm(i)=0;  
i = i +1;  
Screen('FillRect',wPtr,black, [0 0 1680 1050]);  
Screen(wPtr, 'Flip');  
end  
while etime(clock, t0) \< secs  
disp(clock);  
white=WhiteIndex(wPtr);  
pwm(i)=1;  
i = i +1;  
Screen('FillRect',wPtr,black);  
Screen('FillRect',wPtr,white, [0 0 1680 1050] );  
Screen(wPtr, 'Flip');  
end  
end  
Screen('Close', wPtr);  
%%%%%%  
  
Screen hangs and becomes nonresponsive.  
  
Any ideas or hints for the problem ?  
  
Thanks!

---

<div class="post-metadata">

**Author:** ![dcnieho](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dcnieho](https://psychtoolbox.discourse.group/u/dcnieho)\
**Post date:** [November 22, 2011, 2:00am UTC](https://psychtoolbox.discourse.group/t/why-does-this-code-hang/468/2 "2011-11-22T02:00:46Z")

</div>

yes, you're using the worst possible way of stimulus timing, it bruns  
all your cpu cycles causing windows to flag your app as  
non-responsive.  
  
Read the tutorial pdf that comes with PTB and especially learn about  
Screen('Flip')'s when parameter, thats what you should use for timing.  
  
Best,  
Dee  
  

> On 22/11/2011, Yigit Turgut \<[y.turgut@...](mailto:y.turgut@...)\> wrote:  
> \> Hi everyone,  
> \>  
> \> When I run the following code ;  
> \>  
> \> %%%%%%  
> \> screen=0;  
> \> [wPtr,rect]=Screen('OpenWindow',screen, [], []);  
> \> HideCursor;  
> \> tic  
> \> freq\_Hz = 20 ;  
> \> secs = 1 / freq\_Hz;  
> \> measurement\_time = 5;  
> \> i=1;  
> \> t0 = clock;  
> \> pwm = zeros(1, 100000);  
> \> while etime(clock, t0) \< measurement\_time  
> \> disp(clock);  
> \> while etime(clock, t0) \< secs  
> \> disp(clock);  
> \> black=BlackIndex(wPtr);  
> \> pwm(i)=0;  
> \> i = i +1;  
> \> Screen('FillRect',wPtr,black, [0 0 1680 1050]);  
> \> Screen(wPtr, 'Flip');  
> \> end  
> \> while etime(clock, t0) \< secs  
> \> disp(clock);  
> \> white=WhiteIndex(wPtr);  
> \> pwm(i)=1;  
> \> i = i +1;  
> \> Screen('FillRect',wPtr,black);  
> \> Screen('FillRect',wPtr,white, [0 0 1680 1050] );  
> \> Screen(wPtr, 'Flip');  
> \> end  
> \> end  
> \> Screen('Close', wPtr);  
> \> %%%%%%  
> \>  
> \> Screen hangs and becomes nonresponsive.  
> \>  
> \> Any ideas or hints for the problem ?  
> \>  
> \> Thanks!  
> \>

---

<div class="post-metadata">

**Author:** ![y33t](https://avatars.discourse-cdn.com/v4/letter/y/50afbb/32.png) [@y33t](https://psychtoolbox.discourse.group/u/y33t)\
**Post date:** [November 23, 2011, 5:42pm UTC](https://psychtoolbox.discourse.group/t/why-does-this-code-hang/468/3 "2011-11-23T17:42:55Z")

</div>

In the slides.pdf came with the install ;  
  
Instead of:  
Screen('FillRect', win, [red1 green1 blue1], [left1 top1 right1 bot1]);  
  
Write:  
mycolors = [red1 green1 blue1; red2 green2 blue2; ... ; redn greenn bluen];  
myrects = [left1 top1 right1 bot1; left2 top2 right2 bot2; ... ; leftn topn rightn botn];  
Screen('FillRect', win, mycolors, myrects);  
  
And I exactly do this as stated.This is how I use ;  
  
Screen('FillRect',wPtr,black, [0 0 1680 1050])  
  
This is how VBLSynctest uses ;  
  
Screen('FillRect', w, mod(i, 255), [pos+20 pos+20 pos+400 pos+400]);  
  
It draws a rectangle with simple animation and I just blink (on/off) screen yet it gives 1 missed flips out of 600 but my code returns 180 missed flips out of 600.  
  
  
  
  
  
  
  

> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "mauro.toroe" \<mauro.toro.e@...\> wrote:  
> \>  
> \>   
> \> Greatings Yigit,  
> \>   
> \> Take a look at:  
> \> [http://docs.psychtoolbox.org/VBLSyncTest](http://docs.psychtoolbox.org/VBLSyncTest)  
> \>   
> \> And "edit VBLSyncTest" to get an idea about how to refresh your stimuli in a real precise way.  
> \>   
> \> Also, try using GetSecs() to get accurate timing meassures, the clock function gives a really weird output, maybe that's what breaking your script.  
> \>   
> \> Suerte!  
> \>   
> \> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), Yigit Turgut \<y.turgut@\> wrote:  
> \> \>  
> \> \> Hi everyone,  
> \> \>   
> \> \> When I run the following code ;  
> \> \>   
> \> \> %%%%%%  
> \> \> screen=0;  
> \> \> [wPtr,rect]=Screen('OpenWindow',screen, [], []);  
> \> \> HideCursor;  
> \> \> tic  
> \> \> freq\_Hz = 20 ;  
> \> \> secs = 1 / freq\_Hz;  
> \> \> measurement\_time = 5;  
> \> \> i=1;  
> \> \> t0 = clock;  
> \> \> pwm = zeros(1, 100000);  
> \> \> while etime(clock, t0) \< measurement\_time  
> \> \> disp(clock);  
> \> \> while etime(clock, t0) \< secs  
> \> \> disp(clock);  
> \> \> black=BlackIndex(wPtr);  
> \> \> pwm(i)=0;  
> \> \> i = i +1;  
> \> \> Screen('FillRect',wPtr,black, [0 0 1680 1050]);  
> \> \> Screen(wPtr, 'Flip');  
> \> \> end  
> \> \> while etime(clock, t0) \< secs  
> \> \> disp(clock);  
> \> \> white=WhiteIndex(wPtr);  
> \> \> pwm(i)=1;  
> \> \> i = i +1;  
> \> \> Screen('FillRect',wPtr,black);  
> \> \> Screen('FillRect',wPtr,white, [0 0 1680 1050] );  
> \> \> Screen(wPtr, 'Flip');  
> \> \> end  
> \> \> end  
> \> \> Screen('Close', wPtr);  
> \> \> %%%%%%  
> \> \>   
> \> \> Screen hangs and becomes nonresponsive.  
> \> \>   
> \> \> Any ideas or hints for the problem ?  
> \> \>   
> \> \> Thanks!  
> \> \>  
> \>
