C# SMO的后台工作人员。完成

C# SMO的后台工作人员。完成,c#,backgroundworker,C#,Backgroundworker,我找到了一些关于如何创建后台工作人员来报告进度的文章,以避免冻结ui,但我运气不好。这就是我要说的 progressBar on Form1.cs BackupTypes.cs - separate class 我使用以下命令从BackupTypes.cs向progressBar报告进度 bkup.PercentComplete += new PrecentEcompleteEventHandler(Event_PercentComplete); 这是活动完成的百分比 private sta

我找到了一些关于如何创建后台工作人员来报告进度的文章,以避免冻结ui,但我运气不好。这就是我要说的

progressBar on Form1.cs
BackupTypes.cs - separate class
我使用以下命令从BackupTypes.cs向progressBar报告进度

bkup.PercentComplete += new PrecentEcompleteEventHandler(Event_PercentComplete);
这是活动完成的百分比

private static void Event_PercentComplete(object sender, PercentCompleteEventArgs e)
{
  Form1 form = (Form1)Application.OpenForms["Form1"];
  form.progressBar.Value = e.Percent;
}
如何让backgroundworker报告从BackupTypes.cs到Form1.cs的进度,而不冻结UI

编辑后台工作程序

这是我的背景工作人员,如果我现在正在工作,你知道什么?也许我只是需要离开它一步

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Server databaseServer = new Server(dbServer);
            try
            {
                /**
                 * 
                 * Connection to the database
                 * 
                **/
                databaseServer.ConnectionContext.LoginSecure = false;
                databaseServer.ConnectionContext.Login = dbUser;
                databaseServer.ConnectionContext.Password = dbPass;
                databaseServer.ConnectionContext.Connect();



                if (rbFullBackup.Checked == true)
                {
                    dbType = "FULL";
                    int nDay = Convert.ToInt32(cbExperationDate.SelectedValue);
                    BackupTypes.FullBackup("name", "desc", databaseServer, dbDatabase, tbBackupLocation.Text, nDay, dbType);

                }

                if (rbDifferentialBackup.Checked == true)
                {
                    dbType = "DIFFERENTIAL";
                    int nDay = Convert.ToInt32(cbExperationDate.SelectedValue);
                    BackupTypes.DifferentialBackup("name", "desc", databaseServer, dbDatabase, tbBackupLocation.Text, nDay, dbType);
                }

                if (rbLogBackup.Checked == true)
                {
                    dbType = "LOG";
                    int nDay = Convert.ToInt32(cbExperationDate.SelectedValue);
                    BackupTypes.LogBackup("name", "desc", databaseServer, dbDatabase, tbBackupLocation.Text, nDay, dbType);
                }

                if (rbCompressedBackup.Checked == true)
                {
                    dbType = "COMPRESSED";
                    int nDay = Convert.ToInt32(cbExperationDate.SelectedValue);
                    BackupTypes.LogBackup("name", "desc", databaseServer, dbDatabase, tbBackupLocation.Text, nDay, dbType);
                }

                if (rbMailBackup.Checked == true)
                {
                    dbType = "MAIL";
                    int nDay = Convert.ToInt32(cbExperationDate.SelectedValue);
                    BackupTypes.MailBackup("name", "desc", databaseServer, dbMail, tbBackupLocation.Text, nDay, dbType);
                }

            }
            catch (SqlServerManagementException ex)
            {
                /**
                 * 
                 * Write errors to log file
                 * 
                 **/
                if (LogStatus == true)
                {
                    Output(ex.Message);
                    if (DebugStatus == true)
                    {
                        Output(ex.ToString());
                    }
                    Output(" ");
                }

            }
            finally
            {
                if (databaseServer.ConnectionContext.IsOpen)
                {
                    /**
                     * 
                     * Close the connection to the database
                     * 
                    **/
                    databaseServer.ConnectionContext.Disconnect();
                }
            }

    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar.Value = e.ProgressPercentage;
    }
然后从我的BackupTypes.cs报告更改

   form.backgroundWorker1.ReportProgress(e.Percent);

您需要在DoWork方法中调用BackgroundWorker上的ReportProgress

您的BGW代码在哪里?你试过什么了吗?你不认为给我们看BGW代码有助于解决问题吗?你应该在这里以你的形式创建
BGW
,而不是在其他类中。让表单在
DoWork
处理程序中调用其他类的方法。