简体   繁体   中英

How to show a progress from while loading a long method in c#

I want to show a progress from ie 'Please wait loading...' form while running a method in C#.

I have taken one button in that i have written one method code.when we click on that button then the progress form will open until method has in progress state.

  private void btn_copy_Click(object sender, EventArgs e)
        {
            try
            {
                cn = new SqlConnection(EZsql.Properties.Settings.Default.SQLConnstr);
                if (ddl_db.Text != "")
                {
                    str1 = ddl_db.Text;
                    try
                    {
                        str = "SELECT TABLE_NAME FROM " + ddl_db.Text + ".INFORMATION_SCHEMA.Tables where TABLE_NAME in ('AH','PRODUCT','SSADMIN','PASSWORD','lsCUSTOMER','PRICE','PRICELIST') order by TABLE_NAME";
                        if (cn.State != ConnectionState.Open)
                            cn.Open();
                        cmd = new SqlCommand(str, cn);
                        cmd.CommandType = CommandType.Text;
                        sda = new SqlDataAdapter(cmd);
                        ds = new DataSet();
                        sda.Fill(ds, "sys.Tables");
                        dt1 = ds.Tables[0];
                        i = dt1.Rows.Count;
                    }
                    catch (Exception Ex) { i = 0; }
                    if (i == 7)
                    {
                        try
                        {
                            BackgroundWorker bg = new BackgroundWorker();
                            bg.DoWork += new DoWorkEventHandler(bg_DoWork);
                            bg.WorkerReportsProgress = true;
                            bg.ProgressChanged += new ProgressChangedEventHandler(bg_ProgressChanged);
                            // Start the worker.
                            bg.RunWorkerAsync();
                            // Display the loading form.
                            bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);

                        }
                        catch (Exception EX) { }
                        if (istrue == false)
                        {
                            MessageBox.Show("Copied Sucessfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("Copying Failed", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Some tables are not there in your database  \n Your Database must have 'AH','PRICE','PRICELIST','SSADMIN','PASSWORD','PRODUCT','lsPRICE' tables \n Invalid database", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    MessageBox.Show("Please select the database", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception Ex) { }
            istrue = false;
        }


 private void bg_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

        //some progress report - here you can access your controls
        //this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
    }
    void copy()
    {
        popup pp = new popup();
        pp.ShowDialog();
        if (Chb_Cust.Checked == true)
        {
            try
            {
                str = "delete AH where AC_TYPENO='A' or AC_TYPENO='D'";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                str = "Insert into AH select * from " + str1 + ".dbo.AH where " + str1 + ".dbo.AH.AC_TYPENO='A' or " + str1 + ".dbo.AH.AC_TYPENO='D'";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (Exception EX) { istrue = true; }
        }
        if (Chb_Supp.Checked == true)
        {
            try
            {
                str = "delete AH where AC_TYPENO='B' or AC_TYPENO='C'";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                str = "Insert into AH select * from " + str1 + ".dbo.AH where " + str1 + ".dbo.AH.AC_TYPENO='B' or " + str1 + ".dbo.AH.AC_TYPENO='C'";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (Exception EX) { istrue = true; }
        }
        if (Chb_COA.Checked == true)
        {
            try
            {
                str = "delete AH where AC_TYPENO!='B' and AC_TYPENO!='C' and AC_TYPENO!='A' and AC_TYPENO!='D'";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandType = CommandType.Text;
                cmd.CommandTimeout = 1000;
                cmd.ExecuteNonQuery();
                str = "Insert into AH select * from " + str1 + ".dbo.AH where " + str1 + ".dbo.AH.AC_TYPENO!='B' and " + str1 + ".dbo.AH.AC_TYPENO!='C' and " + str1 + ".dbo.AH.AC_TYPENO!='A' and " + str1 + ".dbo.AH.AC_TYPENO!='D'";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (Exception EX) { istrue = true; }
        }
        if (Chb_Price_Last.Checked == true)
        {
            try
            {
                str = "drop table PRICE,PRICELIST,lsCUSTOMER  ";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandType = CommandType.Text;
                cmd.CommandTimeout = 1000;
                cmd.ExecuteNonQuery();
                str = "select * into PRICE from " + str1 + ".dbo.PRICE";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                str = "select * into PRICELIST from " + str1 + ".dbo.PRICE LIST";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                str = "select * into lsCUSTOMER from " + str1 + ".dbo.lsCUSTOMER";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (Exception EX) { istrue = true; }
        }
        if (Chb_Prod.Checked == true)
        {
            try
            {
                str = "drop table PRODUCT ";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                str = "select * into PRODUCT from " + str1 + ".dbo.PRODUCT";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (Exception EX) { istrue = true; }
        }
        if (Chb_Admin_Setup.Checked == true)
        {
            try
            {
                str = "drop table SSADMIN ";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                str = "select * into SSADMIN from " + str1 + ".dbo.SSADMIN";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (Exception Ex) { istrue = true; }
        }
        if (Chb_User_Pass.Checked == true)
        {
            try
            {
                str = "drop table PASSWORD,PS,PSDESC ";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                str = "select * into PASSWORD from " + str1 + ".dbo.PASSWORD";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                str = "select * into PS from " + str1 + ".dbo.PS";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                str = "select * into PSDESC from " + str1 + ".dbo.PSDESC";
                if (cn.State != ConnectionState.Open)
                    cn.Open();
                cmd = new SqlCommand(str, cn);
                cmd.CommandTimeout = 1000;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (Exception EX) { istrue = true; }
        }
        try
        {
            str = "SP_Collation";
            if (cn.State != ConnectionState.Open)
                cn.Open();
            cmd = new SqlCommand(str, cn);
            cmd.CommandTimeout = 1000;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.ExecuteNonQuery();
        }
        catch (Exception EX) { istrue = true; }
        pp.Close();
        //try
        //{
        //    str = "SP_Collation";
        //    cn.Close();
        //    cn.ConnectionString += ";Connect Timeout=1000";
        //    if (cn.State != ConnectionState.Open)
        //        cn.Open();
        //    cn.ChangeDatabase(str1);
        //    cmd = new SqlCommand(str, cn);
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    cmd.CommandTimeout = 1000;
        //    cmd.ExecuteNonQuery();
        //}
        //catch (Exception EX) { }
    }
    private void bg_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        // you should invoke worker's ReportProgress method iside your copy() method
        worker.ReportProgress((i * 10));
        copy();
    }

 private void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
        object result = e.Result;
        popup pp = new popup();

        }

when ever i click on copy button it will show popup loading form but it has not closed when the process completed and not displaying copied successfully.

I guess the reason for the error is that you access your controls from the copy() method. For reporting progress from a BackgroundWorker you should use its event ProgressChanged .

Please refer to the following HowTo from Microsoft .

You should add:

bg.WorkerReportsProgress = true;
bg.ProgressChanged += new ProgressChangedEventHandler(bg_ProgressChanged);

And modify your bg_DoWork method:

private void bg_DoWork(object sender, DoWorkEventArgs e)
      {
BackgroundWorker worker = sender as BackgroundWorker;
// you should invoke worker's ReportProgress method iside your copy() method
//worker.ReportProgress((i * 10));
//copy();
}

And add a method for progress reporting:

private void bg_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    //some progress report - here you can access your controls
    //this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM