<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Example - Asynchronous Queries</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Pgtcl Reference Manual" HREF="index.html"><LINK REL="UP" TITLE="Example Programs" HREF="pgtcl-examples.html"><LINK REL="PREVIOUS" TITLE="Example - Bytea Escaping - Picture Viewer, Part 2 - View Pictures" HREF="pgtcl-example-picview-esc.html"><LINK REL="NEXT" TITLE="Example - COPY FROM and COPY TO" HREF="pgtcl-example-copy.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META NAME="creation" CONTENT="2004-11-09T00:53:06"></HEAD ><BODY CLASS="SECT1" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Pgtcl Reference Manual: The PostgreSQL Tcl Interface</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="pgtcl-example-picview-esc.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 5. Example Programs</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="pgtcl-example-copy.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="PGTCL-EXAMPLE-ASYNC" >5.11. Example - Asynchronous Queries</A ></H1 ><P >This example uses the experimental asynchronous query commands described in <A HREF="pgtcl-ref-async.html" >Section 4.4</A >. It shows how a query could be sent to the database, and the application could continue processing without blocking while waiting for the results to come back. It also shows part of how a Cancel button might be implemented. Note: this is not a complete script.</P ><DIV CLASS="EXAMPLE" ><A NAME="PGTCL-EXAMPLE-ASYNC-CODE" ></A ><P ><B >Example 5-17. Asynchronous Queries</B ></P ><PRE CLASS="PROGRAMLISTING" >pg_sendquery $conn "SELECT * FROM tables... WHERE conditions ORDER BY ..." # Wait for not-busy condition: while {[pg_isbusy $conn]} { ... Do something here, like check for user input and update the interface... ... Good idea to insert a delay so it doesn't busy-wait ... ... Check for cancel button ... if {$user_pressed_cancel_button} break } # Did the user hit the Cancel button? If so, try to cancel the query. if {$user_pressed_cancel_button} { pg_cancelrequest $conn } # Fetch results until it there are none left (even if canceled): while {[set res [pg_getresult $conn]] != ""} { if {!$user_pressed_cancel_button} { ... do something with the query result $res ... } pg_result $res -clear } </PRE ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="pgtcl-example-picview-esc.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="pgtcl-example-copy.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Example - Bytea Escaping - Picture Viewer, Part 2 - View Pictures</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="pgtcl-examples.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Example - COPY FROM and COPY TO</TD ></TR ></TABLE ></DIV ></BODY ></HTML >