Пример #1
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];

                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.CurrentVersion.ToString(2), 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.SetIndeterminate(false)));
                            }

                            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.lblPleaseWait.Text = "Downloading...";

                    progressDownload.ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("NClass is already updated.", "NClass", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Пример #2
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;
                DatabaseObjects chooseObjects = 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();

                if (!hasErrors)
                {
                    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)
                        {
                            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();

                    Workspace.Default.AddProject(databaseDiagram.ProjectGenerated);
                }
            }
        }