Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > bd5c3d824c3db63ffd9226c15941e6ad > files > 2358

mozart-1.4.0-1mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>5.6 More on Actions: Listeners</TITLE><LINK href="ozdoc.css" rel="stylesheet" type="text/css"></HEAD><BODY><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node24.html#section.widgets-2.events">&lt;&lt; Prev</A></TD><TD><A href="node19.html">- Up -</A></TD><TD><A href="node26.html#section.widgets-2.entry">Next &gt;&gt;</A></TD></TR></TABLE><DIV id="section.widgets-2.action"><H2><A name="section.widgets-2.action">5.6 More on Actions: Listeners</A></H2><P> In previous sections we used procedures or pairs of object and message as actions. Each time an action is invoked, a new thread is created. While this is fine as it comes to efficiency (threads in Oz are light weight), it may cause trouble in that the order in which actions are invoked might be lost: the threads are created in the right order but there is no guarantee that they will run in that order. </P><P> The class <CODE>Tk<SPAN class="keyword">.</SPAN>listener</CODE> fixes this. An instance of a subclass of <CODE>Tk<SPAN class="keyword">.</SPAN>listener</CODE> has a thread of its own in which it serves action messages in order of invocation. For example, in <A name="label239"></A> </P><DL class="anonymous"><DD class="code"><CODE>L&nbsp;={New&nbsp;<SPAN class="keyword">class</SPAN>&nbsp;<SPAN class="type">$</SPAN>&nbsp;<SPAN class="keyword">from</SPAN><SPAN class="type">&nbsp;Tk.listener</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">meth</SPAN>&nbsp;<SPAN class="functionname">b1</SPAN>&nbsp;{Browse&nbsp;b1}&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">meth</SPAN>&nbsp;<SPAN class="functionname">b2</SPAN>&nbsp;{Browse&nbsp;b2}&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;tkInit}<BR>B1={New&nbsp;Tk<SPAN class="keyword">.</SPAN>button&nbsp;tkInit(parent:W&nbsp;text:<SPAN class="string">'One'</SPAN>&nbsp;action:&nbsp;L<SPAN class="keyword">#</SPAN>b1)}<BR>B2={New&nbsp;Tk<SPAN class="keyword">.</SPAN>button&nbsp;tkInit(parent:W&nbsp;text:<SPAN class="string">'Two'</SPAN>&nbsp;action:&nbsp;L<SPAN class="keyword">#</SPAN>b2)}<BR>{Tk<SPAN class="keyword">.</SPAN>send&nbsp;pack(B1&nbsp;B2&nbsp;side:left)}</CODE></DD></DL><P> the methods <CODE>b1</CODE> and <CODE>b2</CODE> are always executed in the same order in which the corresponding buttons are pressed. </P><P> When the <CODE>tkInit</CODE> method of the class <CODE>Tk<SPAN class="keyword">.</SPAN>listener</CODE> is executed, a new thread together with a message stream is created. Whenever an action is invoked, where the object <CODE><I>O</I></CODE> of an object message pair <CODE><I>O</I></CODE><CODE><SPAN class="keyword">#</SPAN></CODE><CODE><I>M</I></CODE> is an instance of <CODE>Tk<SPAN class="keyword">.</SPAN>listener</CODE>, no new thread is created but <CODE><I>M</I></CODE> is appended at the end of the message stream. The thread then serves the message <CODE><I>M</I></CODE> as soon as all previous messages on the stream have been served completely. It serves <CODE><I>M</I></CODE> by executing the object application <CODE>{</CODE><CODE><I>O</I></CODE><CODE>&nbsp;</CODE><CODE><I>M</I></CODE><CODE>}</CODE>. </P><P> An additional message <CODE><I>M</I></CODE> to be served can be given to a listener by the method <CODE>tkServe</CODE>. For example, by </P><DL class="anonymous"><DD class="code"><CODE>{L&nbsp;tkServe(b1)}</CODE></DD></DL><P> the message <CODE>b1</CODE> is served by <CODE>L</CODE>. </P></DIV><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node24.html#section.widgets-2.events">&lt;&lt; Prev</A></TD><TD><A href="node19.html">- Up -</A></TD><TD><A href="node26.html#section.widgets-2.entry">Next &gt;&gt;</A></TD></TR></TABLE><HR><ADDRESS><A href="http://www.ps.uni-sb.de/~schulte/">Christian&nbsp;Schulte</A><BR><SPAN class="version">Version 1.4.0 (20090610)</SPAN></ADDRESS></BODY></HTML>