private void fromHydroServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddServiceFromHydroServerForm frmAddFromHisServer = new AddServiceFromHydroServerForm();

            if (frmAddFromHisServer.ShowDialog(this) == DialogResult.OK)
            {
                if (bgwMain.IsBusy)
                {
                    MessageBox.Show("The background worker is currently busy.  Please try again in a few moments.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    return;
                }

                tcAddService.SelectedTab = tpAddMultiSvcs; //switch to the multi service tab

                HydroServerClient hydroServerClient = frmAddFromHisServer.HisServerConnection;

                // Build parameters to pass to the background worker
                object[] parameters = new object[2];
                parameters[0] = BackgroundWorkerTasks.AddServicesFromHydroServer;
                parameters[1] = hydroServerClient;

                // Set form controls for doing work
                dgvAddServices.ClearSelection();
                SetupFormForWork(true);

                // Start the asynchronous operation
                bgwMain.RunWorkerAsync(parameters);
            }
        }
        /// <summary>
        /// Searches for WaterOneFlow services registered at an HydroServer, and adds info about those services to the data grid view
        /// </summary>
        /// <param name="portalUtils">Object that contains a pointer to the HydroServer chosen by the user</param>
        /// <param name="e">Parameters from the BackgroundWorker</param>
        /// <returns>Parameters (task type, output message, rows to add to view) to be processed by a BackgroundWorker event handler</returns>
        private object[] AddServicesFromHydroServer(HydroServerClient hydroServerClient, DoWorkEventArgs e)
        {
            // Build parameters to pass to the background worker
            object[] parameters = new object[3];
            parameters[0] = BackgroundWorkerTasks.AddServicesFromHydroServer;
            parameters[1] = "Operation cancelled";

            // Get all items registered with the server
            bgwMain.ReportProgress(0, "Getting list of registered services...");
            List <DataServiceInfo> services = (List <DataServiceInfo>)hydroServerClient.GetWaterOneFlowServices();

            // Read the items into rows
            int totalSteps  = services.Count;
            int currentStep = 0;

            List <string[]> rowsToAdd = new List <string[]> ();

            foreach (DataServiceInfo serviceInfo in services)
            {
                if (bgwMain.CancellationPending)
                {
                    e.Cancel = true;
                    return(parameters);
                }

                currentStep++;
                bgwMain.ReportProgress(100 * currentStep / totalSteps, "Reading service info: " + currentStep + " of " + totalSteps + "...");

                // Create an item to add to the data grid view
                rowsToAdd.Add(CreateServiceRow(serviceInfo));
            }

            // Prepare a message to the user
            string message       = "";
            int    servicesAdded = rowsToAdd.Count;

            if (servicesAdded == 0)
            {
                message = "No services found in HydroServer";
            }
            else if (servicesAdded == 1)
            {
                message = "1 service found in HydroServer";
            }
            else
            {
                message = servicesAdded.ToString() + " services found in HydroServer";
            }

            parameters[1] = message;
            parameters[2] = rowsToAdd;

            return(parameters);
        }
        private void btnGetServices_Click(object sender, EventArgs e)
        {
            // Show hourglass while we check the URL
            this.Cursor       = Cursors.WaitCursor;
            lblStatus.Visible = true;
            lblStatus.Update();

            // Check that this is a valid URL
            string serverUrl = txtUrl.Text;

            if (HydroDesktop.MetadataFetcher.WebOperations.IsUrlValid(serverUrl, true) == false)
            {
                this.Cursor       = Cursors.Default;
                lblStatus.Visible = false;
                MessageBox.Show("Please enter a valid URL to a HydroServer",
                                this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            // Connect to the server
            try
            {
                _hisServerClient = new HydroServerClient(serverUrl);
            }
            catch (Exception ex)
            {
                this.Cursor       = Cursors.Default;
                lblStatus.Visible = false;
                MessageBox.Show("Could not connect to HydroServer at " + serverUrl + ".\n" + ex.Message,
                                this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            // Restore the mouse cursor
            this.Cursor       = Cursors.Default;
            lblStatus.Visible = false;

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Update();
        }
        private void bgwMain_DoWork(object sender, DoWorkEventArgs e)
        {
            object[] parameters        = e.Argument as object[];
            BackgroundWorkerTasks task = (BackgroundWorkerTasks)parameters[0];

            if (task == BackgroundWorkerTasks.AddServicesFromHydroPortal)
            {
                HydroPortalUtils portalUtils = parameters[1] as HydroPortalUtils;
                e.Result = AddServicesFromHydroPortal(portalUtils, e);
            }
            else if (task == BackgroundWorkerTasks.AddServicesFromFile)
            {
                string path = parameters[1] as string;
                e.Result = AddServicesFromDataTable(path, e);
            }
            else if (task == BackgroundWorkerTasks.CheckForExisingServices)
            {
                List <DataServiceInfo> rows = parameters[1] as List <DataServiceInfo>;
                e.Result = CheckForServicesInCache(rows, e);
            }
            else if (task == BackgroundWorkerTasks.UpdateDatabase)
            {
                List <DataServiceInfo> services = parameters[1] as List <DataServiceInfo>;
                e.Result = AddServicesToDatabase(services, e);
            }
            else if (task == BackgroundWorkerTasks.AddServicesFromHydroServer)
            {
                HydroServerClient hydroServerClient = parameters[1] as HydroServerClient;
                e.Result = AddServicesFromHydroServer(hydroServerClient, e);
            }
            else
            {
                // Build parameters to pass to the background worker
                object[] outParameters = new object[2];
                outParameters[0] = BackgroundWorkerTasks.Unknown;
                outParameters[1] = "Unknown task provided to background worker";

                e.Result = outParameters;
            }
        }