Databases Reference
In-Depth Information
Connect (to open a connection), Process (to process the cube), and Cancel (to cancel
processing).
To prevent multiple threads from using the same resource simultaneously, we will use a
simple synchronization approach—switching the buttons on and off. The
Process
button
is disabled until the connection is created; the
Cancel
button is disabled until processing
begins. This synchronization technique might not work for everyone. You can use any
other synchronization technique, such as the
Lock
method of the
System.Object
class.
Listing 34.8 uses the
BackgroundWorker
object provided by the .NET Framework.
LISTING 34.8
Asynchronous Processing of a Cube
using System;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.AnalysisServices;
namespace AmoProcessCancel
{
public partial class Form1: Form
{
Server server = null;
BackgroundWorker backgroundWorker = null;
public Form1()
{
InitializeComponent();
//Before the connection is established, only the Connection
//button is enabled.
connectionButton.Enabled = true;
cancelButton.Enabled = false;
processButton.Enabled = false;
//Create BackgroundWorker object.
this.backgroundWorker = new BackgroundWorker();
this.backgroundWorker.WorkerReportsProgress = false;
this.backgroundWorker.WorkerSupportsCancellation = true;
this.backgroundWorker.RunWorkerCompleted += OnCompleted;
this.backgroundWorker.DoWork += OnDoWork;
}
//This function is called when the user clicks the Connection button.
private void connectionButton_Click(object sender, EventArgs e)
{
//Open a connection to the server.
this.server = new Server();
this.server.Connect(“Datasource=localhost;”);
Search WWH ::
Custom Search