0

我发现了一些关于如何创建后台工作人员来报告进度以不冻结 ui 的文章,但我没有运气。这是我正在做的事情

progressBar on Form1.cs
BackupTypes.cs - separate class

我正在使用以下内容从 BackupTypes.cs 向 progressBar 报告进度

bkup.PercentComplete += new PrecentEcompleteEventHandler(Event_PercentComplete);

这是 Event_PercentComplete

private static void Event_PercentComplete(object sender, PercentCompleteEventArgs e)
{
  Form1 form = (Form1)Application.OpenForms["Form1"];
  form.progressBar.Value = e.Percent;
}

如何让后台工作人员报告从 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);
4

1 回答 1

0

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

于 2013-11-12T17:53:03.317 回答