[comp.windows.x] Synchronous vs Asynchrounous

spanki@color.ced.berkeley.edu (Frank Goodman) (02/02/90)

I read the article about synchronous vs asynchronous, and I have a few
question. I have a popup shell with a label as a child, and I want to
use it for messages to the user. I pop it up in a callback, with a message
like "Loading image, please be patient". However, since it is being poped up
in a callback, and I then go off into a long process of loading an image, my
label widget is never getting the text displayed. I believe, this is because
the expose event is sitting in the Q while I load my image, and not getting
processed while the popup is mapped. I call XFlush right after XtPopup,
but that doesn't do it. How can I ge the text to display? I'm going to want
to use this sort of message system throughout the program, I imagine, I will
need to reset the string (via XtSetValues), as well as move the popup
around. These messages are context specific, and therefore the moving, and
labeling routines will be called from callback routines. How can I get my
text to show?

Thanks,
Frank
---------------------------------------------------------------------------
Frank  Goodman				arpa:  spanki@CED.Berkeley.EDU
University of California, Berkeley	  or:  spanki%CED@jade.Berkeley.EDU
College of Environmental Design		uucp:  ...hplabs!ucbvax!ced!spanki 
S.I.S Research Laboratory	 	phone: Novelty item, not necessary
---------------------------------------------------------------------------

hammer@sp29.csrd.uiuc.edu (David Hammerslag) (02/02/90)

In article <1990Feb1.200702.25274@agate.berkeley.edu>,
spanki@color.ced.berkeley.edu (Frank Goodman) writes:
> 
> I read the article about synchronous vs asynchronous, and I have a few
> question. I have a popup shell with a label as a child, and I want to
> use it for messages to the user. I pop it up in a callback, with a message
> like "Loading image, please be patient". However, since it is being poped up
> in a callback, and I then go off into a long process of loading an image, my
> label widget is never getting the text displayed. I believe, this is because
> the expose event is sitting in the Q while I load my image, and not getting
> processed while the popup is mapped. I call XFlush right after XtPopup,
> but that doesn't do it. How can I ge the text to display? I'm going to want
> to use this sort of message system throughout the program, I imagine, I will
> need to reset the string (via XtSetValues), as well as move the popup
> around. These messages are context specific, and therefore the moving, and
> labeling routines will be called from callback routines. How can I get my
> text to show?
> 

I faced a similar problem.  Here is what I did.  (Caveat:  it ain't pretty,
but is seems to work). If anybody is willing to share a more reasonable 
solution, I would like to see it.

notes:
	This is all under Motif.
	standby is an externally defined label
   
XfWorking()
{
    register XEvent event;
    XtAppContext app = _XtDefaultAppContext();
    Widget bboard;
    Window win;

    bboard = XtParent(standby);
    XtManageChild(bboard);  /* this pops up the shell */

    win = XtWindow(bboard);
    do /* be XtMainLoop until the bboard gets an expose event */
    {
	XtAppNextEvent(app, &event);
	XtDispatchEvent(&event);
    } while ((event.xany.window != win) ||
	     (event.type != Expose));

    XFlush(XtDisplay(standby));
}

-------------------------------------------------------------------------------
David Hammerslag            | "Woe to those who are wise in their own
hammer@uicsrd.csrd.uiuc.edu |  eyes and clever in their own sight." Isaiah 5:21
-------------------------------------------------------------------------------

kit@EXPO.LCS.MIT.EDU (Chris D. Peterson) (02/03/90)

[ David Hammerslag (hammer@uicsrd.csrd.uiuc.edu) writes: ]

> I faced a similar problem.  Here is what I did.  (Caveat:  it ain't pretty,
> but is seems to work). If anybody is willing to share a more reasonable 
> solution, I would like to see it.

> XfWorking()
> {
>     register XEvent event;
>     XtAppContext app = _XtDefaultAppContext();
>     Widget bboard;
>     Window win;
> 
>     bboard = XtParent(standby);
>     XtManageChild(bboard);  /* this pops up the shell */
> 
>     win = XtWindow(bboard);
>     do /* be XtMainLoop until the bboard gets an expose event */
>     {
> 	XtAppNextEvent(app, &event);
> 	XtDispatchEvent(&event);
>     } while ((event.xany.window != win) ||
> 	     (event.type != Expose));
> 
>     XFlush(XtDisplay(standby));
> }

This is one of the best solutions I have seen to this problem.  In face using
XtAppAddTimeout() you can even set a global variable to have this loop time out
after a few seconds, so that your application doesn't hang if the window
happens to be covered and doesn't receive an expose event.  


						Chris D. Peterson     
						MIT X Consortium 

Net:	 kit@expo.lcs.mit.edu
Phone:   (617) 253 - 9608	
Address: MIT - Room NE43-213

hammer@sp29.csrd.uiuc.edu (David Hammerslag) (02/04/90)

In article <9002021641.AA24460@expo.lcs.mit.edu>, kit@EXPO.LCS.MIT.EDU
(Chris D. Peterson) writes:
> 
> [ David Hammerslag (hammer@uicsrd.csrd.uiuc.edu) writes: ]
> 
> > I faced a similar problem.  Here is what I did.  (Caveat:  it ain't pretty,
> > but is seems to work). If anybody is willing to share a more reasonable 
> > solution, I would like to see it.
> 
> 
> This is one of the best solutions I have seen to this problem.  In face using
> XtAppAddTimeout() you can even set a global variable to have this loop
time out
> after a few seconds, so that your application doesn't hang if the window
> happens to be covered and doesn't receive an expose event.  
> 
> 
> 						Chris D. Peterson     
> 						MIT X Consortium 

Well, now I have to 'fess up.  The solution I posted was inspired by the way
the same problem was solved in "xman."  No wonder you liked it!

-------------------------------------------------------------------------------
David Hammerslag            | "Woe to those who are wise in their own
hammer@uicsrd.csrd.uiuc.edu |  eyes and clever in their own sight." Isaiah 5:21
-------------------------------------------------------------------------------

brennan@rtp.dg.com (Dave Brennan) (02/07/90)

David Hammerslag recently posted a solution to the synchronous dialog
problem.  Here's a function that Greg Christy used to solve this problem.

This function uses XWindowEvent to wait for the expose event.  However,
this doesn't give you the ability to time out if that expose event doen't
come along as expected.  Chris Peterson mentioned that this could be done
in the loop in the funciton Hammerslag posted.  (BTW, the "XFlush" at the
end of this function was originally "XSync.")

(This is from the motif-talk mailing list dated 31 Oct 89.)

-------- Included text follows ---------

/*
 *   WaitForExpose()
 *
 *   Greg Christy  (greg@mickey.imsd.contel.com)
 *   Contel Federal Systems
 *
 *   October 30, 1989
 *
 *   This routine will wait (i.e., block) until an Expose event occurs
 *   in the widget specified in the argument, process the event with
 *   XtNextEvent() and then synchronize the server to flush out any
 *   requests made during the execution of XtNextEvent().  This has the
 *   effect of waiting for a widget to "appear".
 */

#include <X11/Intrinsic.h>

void
WaitForExpose(w)
Widget w;

{
  XEvent event;
  Display * display = XtDisplay(w);
  Window window = XtWindow(w);


  XWindowEvent(display, window, ExposureMask, &event); /* wait for expose... */

  XtDispatchEvent(&event);	/* process it */

  XFlush(display);		/* flush out any requests made during */
				/* XtDispatchEvent() */
}
--
                                                   _________
Dave Brennan, User Interfaces, Data General Corp. /      brennan@dg-rtp.dg.com 
Research Triangle Park, North Carolina, 27709    /  ...mcnc!rti!dg-rtp!brennan
Hm: (919) 460-5725  Wk: (919) 248-6330 _________/ dave_brennan@rpitsmts.bitnet