Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!ukc!warwick!csuyk From: csuyk@warwick.ac.uk (FUNG Wai Wa) Newsgroups: comp.windows.x Subject: Help! What is the magic of the sleep() statement? Message-ID: <1991Jan9.150326.6248@warwick.ac.uk> Date: 9 Jan 91 15:03:26 GMT Sender: news@warwick.ac.uk (Network news) Organization: Computing Services, Warwick University, UK Lines: 76 Hi, there, I am new to Xlib programming. I am now stuck at a very funny(?) problem that I can't solve. Would any Xlib expert give me a hand? Thanks. The following few statements attempt to create a window and draw a line with some strings. I found that the sleep(1) statement is very crucial for its success. Its presence can make the program work as I expect but its absence make the program map the windows only. No drawing at all, if I run this program from another machine. Occasionally I can get it running in the same workstation where I started X. Would any guru give me some hints to get around this nasty problem? Regards, FUNG W.W. ----cut here #include #include main() { Display *dpy; int scr; Window win; GC gc; XGCValues gcvalues; XSetWindowAttributes attributes; Visual *visual = CopyFromParent; dpy = XOpenDisplay(NULL); /* connect to the default display */ if (dpy != (Display*)NULL) scr = DefaultScreen(dpy); else { printf("Cannot connect to server\n"); exit(0); } /* creating a very simple window */ attributes.background_pixel = BlackPixel(dpy, scr); win = XCreateWindow(dpy, RootWindow(dpy, scr), 100, 100, 500, 500, 1, CopyFromParent, InputOutput, visual, CWBackPixel, &attributes); XMapRaised(dpy, win); XFlush(dpy); /* create the pen */ gcvalues.foreground = WhitePixel(dpy, scr); gc = XCreateGC(dpy, win, GCForeground, &gcvalues); /* nasty stmt that its existence is crucial if this stmt is omitted, there is chance to get the program working in the same workstation that I started X. However, if I compile and execute this program in another machine over the network, only a blank window appeared but no drawing will be done. However, with this sleep stmt, everything OK. Why??? */ sleep(1); XDrawLine(dpy, win, gc, 0, 0, 300, 300); XDrawString(dpy, win, gc, 100, 100, "Xlib", 4); XDrawString(dpy, win, gc, 200, 200, "Hello", 5); XDrawString(dpy, win, gc, 300, 300, "Xlib", 4); XFlush(dpy); while (1); }