/// <summary>
        /// Use this only if the application is installed but
        /// the device entry is not longer found on the server.
        /// </summary>
        public static bool RepostDevice()
        {
            MattimonSQLite.SQLiteClientDatabase db = GetLocalDatabase();
            bool ok = PostDevice(db, out string errmsg);

            if (errmsg != "")
            {
                throw (new Exception(errmsg));
            }

            // Refresh the services
            List <GUI.Controls.UCMattimonServicesGrid.ServiceMetaData> services = GetMattimonServices();

            foreach (GUI.Controls.UCMattimonServicesGrid.ServiceMetaData s in services)
            {
                MyServiceController.StopService(s.Name);
                while (MyServiceController.GetServiceStatus(s.Name) != MyServiceController.ServiceState.Stopped)
                {
                    ;
                }
                MyServiceController.StartService(s.Name);
            }
            return(ok);
        }
        /// <summary>
        /// Initializes MattimonSqlServerDataRequestWorker that will be requesting DeviceServerObjects
        /// </summary>
        private void InitializeMattimonSqlServerDataRequestWorker()
        {
            if (mSqlSrvRequestWrkInit)
            {
                return;
            }

            MattimonSqlServerDataRequestWorker         = new BackgroundWorker();
            MattimonSqlServerDataRequestWorker.DoWork += (s, e) =>
            {
                object[] result = new object[2];
                if (e.Argument is bool loadFromServer)
                {
                    if (loadFromServer)
                    {
                        // Load the selected instances that where stored on the server
                        result[0] = RequestDeviceServerObjects();
                    }
                    else
                    {
                        // Only provide the instances stored on server to retreive
                        // local sql monitoring data : that data may not be yet posted on the server
                        // this is allowing us to see current data that haven't yet been posted by the MattimonService
                        ServerInstance[] instances = LocalReadServerInstances();
                        if (instances != null)
                        {
                            result[0] = new SQLServerObjectProvider().GetDeviceServerObjects(
                                instances, GetDeviceID(), out Exception ex);
                        }
                    }
                    result[1] = loadFromServer;
                    e.Result  = result;
                }
            };
            MattimonSqlServerDataRequestWorker.RunWorkerCompleted += (s, e) =>
            {
                if (e.Error != null)
                {
                    GUI.BitscoreForms.BitscoreMessageBox.Show(this,
                                                              "An error occurred while attempting to post on the server.\n\nError details:\n\n" +
                                                              e.Error.Message + "\n" + e.Error.StackTrace +
                                                              (e.Error.InnerException != null ? "\n\nInner Exception:\n\n" + e.Error.InnerException.Message + "\n" + e.Error.InnerException.StackTrace : "")
                                                              , "Http Request Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                ///
                /// Result acquired
                ///
                if (e.Result != null && e.Result is object[] result)
                {
                    if (!(result[0] is DeviceServerObjects serverObjects))
                    {
                        serverObjects = new DeviceServerObjects();
                    }

                    bool local = Convert.ToBoolean(result[1]);
                    ///
                    /// Handle Server exceptions
                    ///
                    if (serverObjects.Exception != null || serverObjects.HttpRequestException != null)
                    {
                        if (serverObjects.Exception is SqlException sqlex)
                        {
                            GUI.BitscoreForms.BitscoreMessageBox.Show(this,
                                                                      "An error occurred while attempting to post on the server.\n\nError details:\n\n" +
                                                                      sqlex.Message + "\n" + sqlex.StackTrace +
                                                                      (sqlex.InnerException != null ? "\n\nInner Exception:\n\n" + sqlex.InnerException.Message + "\n" + sqlex.InnerException.StackTrace : "")
                                                                      , "Http Request Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        if (serverObjects.HttpRequestException != null)
                        {
                            GUI.BitscoreForms.BitscoreMessageBox.Show(this,
                                                                      "An error occurred while attempting to post on the server.\n\nError details:\n\n" +
                                                                      serverObjects.HttpRequestException.Message + "\n" + serverObjects.HttpRequestException.StackTrace +
                                                                      (serverObjects.HttpRequestException.InnerException != null ? "\n\nInner Exception:\n\n" + serverObjects.HttpRequestException.InnerException.Message + "\n" + serverObjects.HttpRequestException.InnerException.StackTrace : "")
                                                                      , "Http Request Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        return;
                    }
                    ///
                    /// Success
                    ///
                    else
                    {
                        UpdateGUISQLServer(serverObjects, null, false);

                        if (serverObjects.Instances != null)
                        {
                            if (serverObjects.Instances.Length == 0 && MyServiceController.GetServiceStatus("MattimonSQLServerService") == MyServiceController.ServiceState.Running)
                            {
                                new Thread(() =>
                                {
                                    MyServiceController.StopService("MattimonSQLServerService");
                                }).Start();
                            }
                        }
                    }
                }
            };
            mSqlSrvRequestWrkInit = true;
        }
        /// <summary>
        /// Initializes MattimonWebDataUpdateBackgroundWorker that will be posting settings / data on the server
        /// </summary>
        private void InitializeMattimonWebDataUpdateWorker()
        {
            if (!mUpdateWorkerInit)
            {
                MattimonWebDataUpdateBackgroundWorker         = new BackgroundWorker();
                MattimonWebDataUpdateBackgroundWorker.DoWork += (s, e) =>
                {
                    e.Result = UpdateDeviceOptions(this.DeviceOptions);
                };
                MattimonWebDataUpdateBackgroundWorker.RunWorkerCompleted += (s, e) =>
                {
                    btn_settings_postSettings.Enabled = true;

                    if (e.Error != null)
                    {
                        // reset the switch!
                        dvcopt_agentIntervalChanged = false;

                        GUI.BitscoreForms.BitscoreMessageBox.Show(this,
                                                                  "An error occurred while attempting to post on the server.\n\nError details:\n\n" +
                                                                  e.Error.Message + "\n" + e.Error.StackTrace +
                                                                  (e.Error.InnerException != null ? "\n\nInner Exception:\n\n" + e.Error.InnerException.Message + "\n" + e.Error.InnerException.StackTrace:"")
                                                                  , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Close();
                        return;
                    }

                    if (e.Result != null && e.Result is DeviceOptions options)
                    {
                        if (options.HttpRequestException != null)
                        {
                            // reset the switch!
                            dvcopt_agentIntervalChanged = false;


                            //GUI.BitscoreForms.BitscoreMessageBox.Show
                            MessageBox.Show(this,
                                            "An error occurred while attempting to post on the server.\n\nError details:\n\n" +
                                            options.HttpRequestException.Message + "\n" + options.HttpRequestException.StackTrace +
                                            (options.HttpRequestException.InnerException != null ? "\n\nInner Exception:\n\n" + options.HttpRequestException.InnerException.Message + "\n" + options.HttpRequestException.InnerException.StackTrace : "")
                                            , "Http Request Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Close();
                            return;
                        }
                        if (options.Exception != null)
                        {
                            // reset the switch!
                            dvcopt_agentIntervalChanged = false;

                            //GUI.BitscoreForms.BitscoreMessageBox.Show(this,
                            MessageBox.Show(this,
                                            "An error occurred while attempting to post on the server.\n\nError details:\n\n" +
                                            options.Exception.Message + "\n" + options.Exception.StackTrace +
                                            (options.Exception.InnerException != null ? "\n\nInner Exception:\n\n" + options.Exception.InnerException.Message + "\n" + options.Exception.InnerException.StackTrace : "")
                                            , "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Close();
                            return;
                        }
                        if (options.MySqlExceptionMessage != null)
                        {
                            // reset the switch!
                            dvcopt_agentIntervalChanged = false;

                            GUI.BitscoreForms.BitscoreMessageBox.Show(this,
                                                                      "An error occurred while attempting to post on the server due to a database error.\n\nError details:n\n" + options.MySqlExceptionMessage,
                                                                      "Server Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Close();
                            return;
                        }

                        // If the interval was changed, restart the Agent Service.
                        if (dvcopt_agentIntervalChanged)
                        {
                            try
                            {
                                MyServiceController.StopService("MattimonAgentService");
                                if (MyServiceController.GetServiceStatus("MattimonAgentService") != MyServiceController.ServiceState.Running)
                                {
                                    MyServiceController.StartService("MattimonAgentService");
                                }
                            }
                            catch (Exception svcRestartError)
                            {
                                GUI.BitscoreForms.BitscoreMessageBox.Show(this,
                                                                          ExceptionHelper.GetFormatedExceptionMessage(svcRestartError),
                                                                          "Service Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }

                            // and reset the switch!
                            dvcopt_agentIntervalChanged = false;
                        }

                        // Assign global Device Options
                        this.DeviceOptions = options;

                        // Refresh the form to make sure the data were actually posted
                        Btn_settings_refreshSettings_Click(btn_settings_refreshSettings, EventArgs.Empty);

                        GUI.BitscoreForms.BitscoreMessageBox.Show(this,
                                                                  "Your settings have been succesfully posted on the server.", Text, MessageBoxButtons.OK, MessageBoxIcon.None);
                    }
                };
                mUpdateWorkerInit = true;
            }
        }