July 31, 2002
ElementQueue
Here's a useful construct for Groove tool developers, especially when working with third-party ActiveX controls for UI. The ElementQueue is really easy to use, but I'll preface this with some chat about MVC and threading to give an example of where I needed it. I'm sure you'll have other applications. In the MVC (model-view-controller) structure, your tool will receive events from the model when its dataset changes - locally (because you did something at the UI, usually) or remotely (because another member in the space triggered a data change, which was disseminated to your device). Typically, you'll install some listeners in your controller (UI gluecode) to process these events when they happen, and change the current state of the UI to reflect the new information. When your UI is built with an ActiveX control, you can hit threading issues. This is especially important where your ActiveX control is a facade onto a single-process application (ie. the control silently loads a full application in background): I've seen this with Acrobat and MapPoint. The pipe between "controller" and "view" might need to play nice. Groove runs a single thread for UI, and several background threads for everything else. So when Dynamics processes commands for an Engine (your Model), that's always on one of the background worker threads. The engine then fires a COM event, to which your controller code (in the UI thread) is listening. Groove proxies the event onto the UI thread for your gluecode to pick up. Then you handle the event, by talking to your user interface. Like this (JavaScript): function PropertyList_OnPropertyChanged( sName, pValue, bRemote ) Let's say that your UI is an ActiveX control, and it's busy right now. It won't be able to process your call to its .DoStuff() method (or whatever), and the default behaviour in these circumstances seems to be: die disgracefully. So we really want to queue the event until a later time when we know the UI has settled down. For this, I use an "idle handler" (although you could easily use a timer instead). By registering as an idle handler, you have a function be called whenever the Groove UI thread has some spare time - this seems to be roughly every 20 milliseconds. The idle handler is a great place to perform occasional UI update, by reading from a queue of work-to-do. So let's set up an idle handler:
Now to set up an event queue, and a HandleQueuedCommands() function to dequeue work items. Groove's ElementQueue API is just that: a FIFO queue of XML elements (children of a parent XML element; in this case, a temporary element created just to hold the queue). You can enqueue arbitrary pieces of XML, and dequeue them later. Since the payload is just XML, it's easy to wrap an event and its parameters into an element for the queue. You might want to queue many different types of command; just use elements with different names. It's not necessary to declare a formal schema for these elements, but naming them sensibly helps keep your code readable. So, in Initialize(),
When you want to enqueue something - for example, an event called from your datamodel - just wrap it into an element and place it in the queue:
Then, when the UI is idle, look at the queue and handle any commands which we see there. You'll get the best performance if you process the whole queue in the idle handler (rather than just handling one queued command at a time).
function HandleQueuedCommand( pElement ) To recap:
Of course there are several other ways to use an element queue. The queue could be persisted, or even disseminated (on an element in a PropertyList, for example) - that could be a nice foundation for a distributed-processing application. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vcard
archives: January 2005 December 2004 November 2004 October 2004 September 2004 August 2004 July 2004 June 2004 May 2004 April 2004 March 2004 February 2004 January 2004 December 2003 November 2003 October 2003 September 2003 August 2003 July 2003 June 2003 May 2003 April 2003 March 2003 February 2003 January 2003 December 2002 November 2002 October 2002 September 2002 August 2002 July 2002 June 2002 May 2002 April 2002 March 2002 February 2002 January 2002 December 2001 November 2001 October 2001 September 2001 August 2001 July 2001 June 2001 see also: {groove: [ ray, matt, paresh, mike, jeff, john ], other: [ /* more blogroll to follow */ ] } The views expressed on this weblog are mine alone and do not necessarily reflect the views of my employer. RSS 2.0 RSS 1.0 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||