Databases Reference
In-Depth Information
As in Listing 34.8, we use the BackgroundWorker class provided by .NET Framework. We
also use the button state for synchronization:
public AmoTraceForm()
{
InitializeComponent();
//Until the connection is established, only the
//Connection button is enabled.
SetButtonsState(ButtonState.Connect);
//Create and initialize BackgroundWorker.
InitializeBackgroundWorker();
}
//Reset the state of the buttons.
private void SetButtonsState(ButtonState state)
{
switch (state)
{
case ButtonState.Connect:
processButton.Enabled = false;
connectionButton.Enabled = true;
break;
case ButtonState.Process:
connectionButton.Enabled = false;
processButton.Enabled = true;
break;
case ButtonState.None:
connectionButton.Enabled = false;
processButton.Enabled = false;
break;
}
}
private void InitializeBackgroundWorker()
{
this.backgroundWorker = new BackgroundWorker();
this.backgroundWorker.WorkerReportsProgress = false;
this.backgroundWorker.WorkerSupportsCancellation = true;
this.backgroundWorker.RunWorkerCompleted += OnCompleted;
this.backgroundWorker.DoWork += OnDoWork;
}
When a user clicks the Connect button, we connect to the server:
private void connectionButton_Click(object sender, EventArgs e)
{
Search WWH ::




Custom Search