Update main form after closing the load form

Asked

Viewed 46 times

1

What I’m trying to do is, I need to open another form in another thread where there is a Progress bar that is indeterminated, and in the main ui I need to update the datagrid with the data from the bd after the load is finished.

 private async void Disp_data_Sim()
        {
            var windowToOpen = new WaitingWorker()
            {
                Owner = this,
            };

            await Task.Run(new Action(() =>
            {
                this.BeginInvoke((MethodInvoker)delegate
                {

                    windowToOpen.ShowDialog();
                });
                try
                {
                    var tempCon = File.ReadAllText("DBConnection.json");
                    var tempCon1 = Crypt.Decrypt(tempCon, "encryption");
                    var sqlInfo = new JavaScriptSerializer().Deserialize<SQLInfo>(tempCon1);
                    using (SqlConnection con = new SqlConnection(sqlInfo.GetConString()))
                    {
                        con.Open();
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            cmd.CommandText = "SELECT referencia,descricao,pr_custo1,etiqueta,qtd FROM Etiquetas Where etiqueta = @etiqueta";
                            cmd.Parameters.AddWithValue("@etiqueta", 'S');
                            DataTable dtbl = new DataTable();
                            SqlDataAdapter da = new SqlDataAdapter(cmd);
                            da.Fill(dtbl);

                            dataGridView1.Invoke(new Action(() => dataGridView1.DataSource = dtbl));
                        }
                        con.Close();

                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                this.Invoke((MethodInvoker)delegate
                {

                    windowToOpen.Close();
                });
            }));
        }

The problem is this, it runs another form but uses the primary so the program is "unavailable".

  • Look for async this way of doing it is overcome and difficult. In some cases the attempt to backfire and get worse: https://answall.com/q/1946/101

  • I do not want to gain speed, what I want is not to look at the program that does not respond for 10-15sec while the program goes to fetch everything the comic.

1 answer

0

I use something similar, I have a form that displays a gif image with a message to wait for the client.

What I do in this case is to open the waiting form with .Show() and not ShowModal(). And when you want to finish the execution, you can give a Dispose() on the form.

It would look basically like this (I did not test):

private async void Disp_data_Sim()
{
    var windowToOpen = new WaitingWorker()
    {
        Owner = this,
    };

    windowToOpen.Show();

    try
    {
        var tempCon = File.ReadAllText("DBConnection.json");
        var tempCon1 = Crypt.Decrypt(tempCon, "encryption");
        var sqlInfo = new JavaScriptSerializer().Deserialize<SQLInfo>(tempCon1);
        using (SqlConnection con = new SqlConnection(sqlInfo.GetConString()))
        {
            con.Open();
            using (SqlCommand cmd = con.CreateCommand())
            {
                cmd.CommandText = "SELECT referencia,descricao,pr_custo1,etiqueta,qtd FROM Etiquetas Where etiqueta = @etiqueta";
                cmd.Parameters.AddWithValue("@etiqueta", 'S');
                DataTable dtbl = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dtbl);

                dataGridView1.Invoke(new Action(() => dataGridView1.DataSource = dtbl));
            }
            con.Close();
        }

        windowToOpen.Close();
        //---- ou  windowToOpen.Dispose();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

As you have only one indeterminated progressbar this solution will already solve.

However, if you want to place a progressibar that is actually updated along with the information that is processed in the source form (or any external method), you will need to work with INotifyPropertyChanged to update the progressibar positions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.