Ejemplo n.º 1
0
        private void mnuFromDatabase_Click(object sender, EventArgs e)
        {
            var connectionDialog = new ConnectionDialog();

            if (connectionDialog.ShowDialog() == DialogResult.OK)
            {
                // Initialize the dialog that will contain the progress bar
                ProgressDialog progressDialog = new ProgressDialog();

                // Set the dialog to operate in indeterminate mode
                progressDialog.SetIndeterminate(true);

                DatabaseCSharpDiagramGenerator databaseDiagram = null;
                bool hasErrors = false;

                // Initialize the thread that will handle the background process
                Thread backgroundThread = new Thread(
                    new ThreadStart(() =>
                {
                    try
                    {
                        databaseDiagram = new DatabaseCSharpDiagramGenerator(connectionDialog.Connection);

                        Thread.Sleep(500);
                    }
                    catch (Exception ex)
                    {
                        hasErrors = true;

                        Invoke(new MethodInvoker(() =>
                        {
                            MessageBox.Show(
                                this,
                                ex.Message,
                                Translations.Strings.Error,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                        }));
                    }
                    finally
                    {
                        // Close the dialog if it hasn't been already
                        if (progressDialog.InvokeRequired)
                        {
                            progressDialog.BeginInvoke(new Action(() => progressDialog.Close()));
                        }
                    }
                }));

                // Sets to single thread apartment (STA) mode before OLE calls
                backgroundThread.SetApartmentState(ApartmentState.STA);

                // Start the background process thread
                backgroundThread.Start();

                // Open the dialog
                progressDialog.ShowDialog();

                DatabaseObjects chooseObjects = new DatabaseObjects(databaseDiagram.Tables, databaseDiagram.Views);

                if (chooseObjects.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    databaseDiagram.Tables = chooseObjects.Tables;
                    databaseDiagram.Views  = chooseObjects.Views;
                    databaseDiagram.ConvertToPascalCase = chooseObjects.ConvertToPascalCase;
                }
                else
                {
                    return;
                }

                progressDialog = new ProgressDialog();

                progressDialog.SetIndeterminate(true);

                backgroundThread = new Thread(
                    new ThreadStart(() =>
                {
                    try
                    {
                        databaseDiagram.Generate();

                        Thread.Sleep(500);
                    }
                    catch (Exception ex)
                    {
                        hasErrors = true;

                        Invoke(new MethodInvoker(() =>
                        {
                            MessageBox.Show(
                                this,
                                ex.Message,
                                Translations.Strings.Error,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                        }));
                    }
                    finally
                    {
                        if (progressDialog.InvokeRequired)
                        {
                            progressDialog.BeginInvoke(new Action(() => progressDialog.Close()));
                        }
                    }
                }));

                backgroundThread.SetApartmentState(ApartmentState.STA);

                backgroundThread.Start();

                progressDialog.ShowDialog();

                if (!hasErrors)
                {
                    Workspace.Default.AddProject(databaseDiagram.ProjectGenerated);
                }
            }
        }
Ejemplo n.º 2
0
        private void mnuCheckForUpdates_Click(object sender, EventArgs e)
        {
            System.Threading.Tasks.Task <IReadOnlyList <Octokit.Release> > releases;
            Octokit.Release latest = null;

            ProgressDialog progressDownload = new ProgressDialog();

            Thread thread = new Thread(() =>
            {
                Octokit.GitHubClient client = new Octokit.GitHubClient(new Octokit.ProductHeaderValue("alexgracianoarj"));
                releases = client.Repository.Release.GetAll("alexgracianoarj", "nclass");
                latest   = releases.Result[0];
                Console.Write(latest.Name);

                if (progressDownload.InvokeRequired)
                {
                    progressDownload.BeginInvoke(new Action(() => progressDownload.Close()));
                }
            });

            thread.Start();

            progressDownload.Text = "Update";
            progressDownload.lblPleaseWait.Text = "Checking...";
            progressDownload.SetIndeterminate(true);

            progressDownload.ShowDialog();

            double latestVersion  = Convert.ToDouble(latest.TagName.Replace("v", ""), System.Globalization.CultureInfo.InvariantCulture);
            double programVersion = Convert.ToDouble(Program.GetVersionString().Split(' ')[1], System.Globalization.CultureInfo.InvariantCulture);

            if (latestVersion > programVersion)
            {
                if (MessageBox.Show("There is a new version of NClass.\n\nDo you want download the new version and install it now?", "NClass", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
                {
                    thread = new Thread(() =>
                    {
                        WebClient wc = new WebClient();

                        wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler((sen, env) =>
                        {
                            double bytesIn    = double.Parse(env.BytesReceived.ToString());
                            double totalBytes = double.Parse(env.TotalBytesToReceive.ToString());
                            double percentage = bytesIn / totalBytes * 100;

                            if (progressDownload.InvokeRequired)
                            {
                                progressDownload.BeginInvoke(new Action(() => progressDownload.lblPleaseWait.Text = "Downloaded " + Convert.ToInt32(percentage) + "% - " + (env.BytesReceived / 1024) + " KB of " + (env.TotalBytesToReceive / 1024) + " KB"));
                            }

                            if (progressDownload.InvokeRequired)
                            {
                                progressDownload.BeginInvoke(new Action(() => progressDownload.progressBar1.Value = Convert.ToInt32(percentage)));
                            }
                        });

                        wc.DownloadFileCompleted += new AsyncCompletedEventHandler((sen, env) =>
                        {
                            // Close the dialog if it hasn't been already
                            if (progressDownload.InvokeRequired)
                            {
                                progressDownload.BeginInvoke(new Action(() => progressDownload.Close()));
                            }

                            System.Diagnostics.Process.Start(Path.GetTempPath() + "NClass_Update.exe");

                            Application.Exit();
                        });

                        wc.DownloadFileAsync(new Uri(latest.Assets[0].BrowserDownloadUrl), Path.GetTempPath() + "NClass_Update.exe");
                    });

                    thread.Start();

                    progressDownload.Text = "Update";
                    progressDownload.lblPleaseWait.Text = "Downloading...";
                    progressDownload.SetIndeterminate(false);

                    progressDownload.ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("NClass already is updated.", "NClass", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Ejemplo n.º 3
0
        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (isProcessRunning)
            {
                return;
            }

            IList <DatabaseDbSchema> schemas = new List <DatabaseDbSchema>();

            string  connectionStr = connectionStringTextBox.Text.Trim();
            SqlType serverType    = (SqlType)serverTypeComboBox.SelectedItem;

            // Initialize the dialog that will contain the progress bar
            ProgressDialog progressDialog = new ProgressDialog();

            // Set the dialog to operate in indeterminate mode
            progressDialog.SetIndeterminate(true);

            // Initialize the thread that will handle the background process
            Thread backgroundThread = new Thread(
                new ThreadStart(() =>
            {
                try
                {
                    isProcessRunning = true;

                    //Create the database reader object.
                    var metadataReader = new DatabaseReader(connectionStr, serverType);
                    schemas            = metadataReader.AllSchemas();

                    if (cboSchema.InvokeRequired)
                    {
                        cboSchema.BeginInvoke(new Action(() =>
                        {
                            cboSchema.DataSource = schemas.Select(x => x.Name).ToList();
                        }));
                    }

                    Thread.Sleep(500);
                }
                catch (Exception ex)
                {
                    Invoke(new MethodInvoker(() =>
                    {
                        MessageBox.Show(
                            this,
                            ex.Message,
                            Translations.Strings.Error,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                    }));
                }
                finally
                {
                    isProcessRunning = false;

                    if (progressDialog.InvokeRequired)
                    {
                        progressDialog.BeginInvoke(new Action(() => progressDialog.Close()));
                    }
                }
            }));

            // Sets to single thread apartment (STA) mode before OLE calls
            backgroundThread.SetApartmentState(ApartmentState.STA);

            // Start the background process thread
            backgroundThread.Start();

            // Open the dialog
            progressDialog.ShowDialog();
        }