如何在c#中加载长方法时显示进度
本文关键字:方法 显示 加载 | 更新日期: 2023-09-27 18:18:12
我想显示一个进度,即'请等待加载…
我已经采取了一个按钮,我已经写了一个方法代码。当我们点击该按钮时,进度表单将打开,直到方法处于进度状态。
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();
}
当我点击复制按钮时,它会显示弹出加载表单,但是当过程完成时它没有关闭,也没有显示复制成功。
我猜错误的原因是您从copy()方法访问控件。要从BackgroundWorker报告进度,应该使用它的事件ProgressChanged。
请参考以下微软的指南
你应该加上:
bg.WorkerReportsProgress = true;
bg.ProgressChanged += new ProgressChangedEventHandler(bg_ProgressChanged);
并修改bg_DoWork方法:
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_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//some progress report - here you can access your controls
//this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
}