Problem with using sleep in a loop
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Problem with using sleep in a loop




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Problem with using sleep in a loop  
Tomdumon@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-16-07 06:37 AM

Hey,

I've been trying to fix this for a while. I was writing a code to send
a buttonpress in X, left arrow then right arrow. I managed to get that
working but it was a bit too rapid so I decided to add a sleep
inbetween that's where the trouble began. It doesn't do anything, it
doesn't get to the loop. I was thinking it might be to do with the
signals (it works fine without a delay function). I've even tried
replacing sleep with other things like comparing cpu times or loops
but to no success.

Anyway here's the source:
#include <stdio.h>
#include <X11/extensions/XTest.h>
#include <X11/Xlib.h>
#include <unistd.h>

Display *dpy;

int main()
{
sleep(4);

if ((dpy = XOpenDisplay("Glorioso.localdomain:0.0")) == NULL){
printf("It XXXXed up");
exit(1);
}
while(1){

XTestFakeKeyEvent(dpy, 100, True, CurrentTime);
XTestFakeKeyEvent(dpy, 100, False, CurrentTime);
sleep(2);
XTestFakeKeyEvent(dpy, 102, True, CurrentTime);
XTestFakeKeyEvent(dpy, 102, False, CurrentTime);
sleep(2);
}

XCloseDisplay(dpy);
}





[ Post a follow-up to this message ]



    Re: Problem with using sleep in a loop  
Måns Rullgård


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-16-07 12:32 PM

Tomdumon@gmail.com writes:

> Hey,
>
> I've been trying to fix this for a while. I was writing a code to send
> a buttonpress in X, left arrow then right arrow. I managed to get that
> working but it was a bit too rapid so I decided to add a sleep
> inbetween that's where the trouble began. It doesn't do anything, it
> doesn't get to the loop. I was thinking it might be to do with the
> signals (it works fine without a delay function). I've even tried
> replacing sleep with other things like comparing cpu times or loops
> but to no success.
>
> Anyway here's the source:
> #include <stdio.h>
> #include <X11/extensions/XTest.h>
> #include <X11/Xlib.h>
> #include <unistd.h>
>
> Display *dpy;
>
> int main()
> 	{
> 	sleep(4);
>
> 	if ((dpy = XOpenDisplay("Glorioso.localdomain:0.0")) == NULL){
>                 	printf("It XXXXed up");
> 			exit(1);
> 			}
> 	while(1){
>
> 		XTestFakeKeyEvent(dpy, 100, True, CurrentTime);
> 		XTestFakeKeyEvent(dpy, 100, False, CurrentTime);
> 		sleep(2);
> 		XTestFakeKeyEvent(dpy, 102, True, CurrentTime);
> 		XTestFakeKeyEvent(dpy, 102, False, CurrentTime);
> 		sleep(2);
> 		}
>
> 	XCloseDisplay(dpy);
> }

Xlib is probably buffering the messages to the server.  Try adding an
XFlush(dpy) call before each sleep().

--
Måns Rullgård
mans@mansr.com





[ Post a follow-up to this message ]



    Re: Problem with using sleep in a loop  
Tomdumon@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-16-07 12:32 PM

On 16 Dec, 11:33, M=E5ns Rullg=E5rd <m...@mansr.com> wrote:
> Tomdu...@gmail.com writes: 
> 
> 
> 
> 
> 
> 
> 
>
> Xlib is probably buffering the messages to the server.  Try adding an
> XFlush(dpy) call before each sleep().
>
> --
> M=E5ns Rullg=E5rd
> m...@mansr.com

Thanks , would you mind explaining it a bit more so I don't mess up
in future?





[ Post a follow-up to this message ]



    Re: Problem with using sleep in a loop  
John Tsiombikas


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-16-07 06:26 PM

On 2007-12-16, Tomdumon@gmail.com <Tomdumon@gmail.com> wrote:
> 
>
> Thanks , would you mind explaining it a bit more so I don't mess up
> in future?

Xlib implements an asyncronous communication model with the X server.
When you call an Xlib function that corresponds to an X11 protocol
request, that request is appended to a local request buffer. Then when
enough of them have been collected, they are transmited all together to
the X server.

For that reason, two syncronization functions are provided by Xlib:
XFlush, and XSync.

XFlush flush the request buffer, forcing their transmission, and return
to the caller.
XSync will flush the buffer, then wait for acknowledgment from the X
server that all the requests have been processed, before returning.

Make sure to not call these two functions, unless absolutely necessary,
as they will slow down things considerably (especially XSync).

--
John Tsiombikas (Nuclear / Mindlapse)
http://nuclear.sdf-eu.org/





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:03 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register