Databases Reference
In-Depth Information
FIGURE 33.20
The user interface displays the top-selling products in the product family the
user selected.
Client applications that support canceling do not usually execute commands in the main
thread, but instead create a special worker thread specifically for the purpose of executing
long-running operations while the user is working on something else.
ADOMD.NET provides the AdomdCommand.Cancel method that allows a client application
to cancel a command that it sent to the server. ADOMD.NET cancels the commands by
creating a new connection to the server and sending a <Cancel> XML/A request. (This
method works only in the context of one user's session on the server. Another user
cannot cancel the first user's command. For more information about the Cancel
command, see Chapter 26, “Server Architecture and Command Execution.”) A client
application should call Cancel on a thread different from the one on which execution
was started because ADOMD.NET methods that execute queries— ExecuteCellSet ,
ExecuteDataReader , Execute , and so on—don't return to the caller until execution of the
command is complete. We will create a simple client application that makes it possible
for the user to cancel a command. Before we begin, however, we want to point out that
the operation of connecting to a data source can take a long time and things will work
better if it is executed in separate thread. For the sake of simplicity, we do not show that
in the example.
Listing 33.29 shows the execution and cancellation of a command in asynchronous mode.
This client application has three buttons: Open Connection (to open a connection),
Execute (to execute the command), and Cancel (to cancel the command). It also has a
simple list box for displaying the member names on the screen. (We could have used a
grid control, but we didn't, to avoid complicating the example.)
When you work with multiple threads, it is important to provide synchronization
between objects and to prevent multiple threads from using the same resource simultane-
ously. We do this by turning the buttons on and off. For example, the Execute button is
disabled while the connection is being made. The Cancel button is disabled when the user
clicks the Execute button and remains disabled until the command starts to execute. At
this point, the Execute button is disabled and the Cancel button is enabled. This prevents
the possibility that the command would be executed twice.
Search WWH ::




Custom Search