private DbObjectScripterResult ScriptObjects(DbObjectScripterArgs args)
        {
            DbObjectScripterResult result = new DbObjectScripterResult();

            _scripter = new DbObjectScripter(args.cp);
            _scripter.ScriptingProgress += new ScriptingProgressDelegate(OnScriptingProgress);
            _scripter.ObjectTypes        = args.objectTypes;
            _scripter.DbObjectSearchType = args.searchType;
            _scripter.SearchText         = args.searchText;

            string script = String.Empty;

            switch (args.destination)
            {
            case ScriptDestination.Window:
                result.errors = _scripter.ScriptObjects(out script);
                result.script = script;
                break;

            case ScriptDestination.File:
                result.errors = _scripter.ScriptObjects(out script);
                result.script = script;
                break;

            case ScriptDestination.Folder:
                result.errors = _scripter.ScriptObjectsToFolder(args.path);
                break;
            }
            return(result);
        }
        private void DoBackgroundWork(object sender, DoWorkEventArgs e)
        {
            // Do not access the form's BackgroundWorker reference directly.
            // Instead, use the reference provided by the sender parameter.
            BackgroundWorker bw = sender as BackgroundWorker;

            // Extract the argument.
            DbObjectScripterArgs args = (DbObjectScripterArgs)e.Argument;


            // Start the time-consuming operation.
            _isInProgress = true;

            e.Result = ScriptObjects(args);

            // If the operation was canceled by the user,
            // set the DoWorkEventArgs.Cancel property to true.
            if (bw.CancellationPending)
            {
                e.Cancel = true;
            }
        }
        private void StartScripting( )
        {
            if (_isInProgress)
            {
                CancelScripting();
            }

            while (_workerThread.IsBusy)
            {
                Application.DoEvents();
            }

            if (lv.CheckedItems.Count == 0)
            {
                MessageBox.Show("Please select object types first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DbObjectScripterArgs args = new DbObjectScripterArgs();

            args.destination = _destination;
            foreach (ListViewItem item in lv.CheckedItems)
            {
                args.objectTypes.Add((ScriptObjectTypes)item.Tag);
            }

            switch (_destination)
            {
            case ScriptDestination.Folder:
                if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                _destPath = folderBrowserDialog1.SelectedPath;
                break;

            case ScriptDestination.File:
                if (saveFileDialog1.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                _destPath = saveFileDialog1.FileName;
                break;

            default:
                _destPath = String.Empty;
                break;
            }
            args.path       = _destPath;
            args.cp         = _connParams;
            args.searchText = txtSearchText.Text;
            args.searchType = (SearchType)cmbSearchType.SelectedItem;

            btnStart.Enabled  = false;
            btnClose.Enabled  = false;
            btnStop.Enabled   = true;
            groupBox1.Enabled = false;
            groupBox2.Enabled = false;
            groupBox3.Enabled = false;

            lblStatus.Text = "Scripting in progress...";
            _workerThread.RunWorkerAsync(args);
        }