shah Momin

shah Momin

  • NA
  • 176
  • 22.3k

System.InvalidOperationException: 'This BackgroundWorker states

Jun 5 2022 1:04 PM

System.InvalidOperationException: 'This BackgroundWorker states that it doesn't report progress. Modify WorkerReportsProgress to state that it does report progress.'

private void GenerateBackup(object sender, DoWorkEventArgs e)
{
    ConfigurationBLL configurationBLL = new ConfigurationBLL();

    BackgroundWorker backgroundWorker = sender as BackgroundWorker;
    string fileName = (string)(e.Argument as Dictionary<string, object>)["fileName"];
    backgroundWorker.ReportProgress(-1,(object) "Generating Database Backup...");
    configurationBLL.GetBackupData(dtpFromDateTime.Value, dtpToDateTime.Value, fileName);
    backgroundWorker.ReportProgress(100, (object)"Backup Database Completed");
}

 Form Code 1 Error In Line Number 7

private void BtnGenrateBackup_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.Filter = "backup files (*.bak)|*.bak|All files (*.*)|*.*";
    saveFileDialog.FileName = "Database" + DateTime.Now.ToString("yyyy-MM-dd");
    if (DialogResult.OK != saveFileDialog.ShowDialog())
        return;
    LoadingBar loadingBar = new LoadingBar("Database Backup", new DoWorkEventHandler(GenerateBackup), new Dictionary<string, object>()
    {
        {
            "fileName",(object)saveFileDialog.FileName
        }
    });
    int num1 = (int)loadingBar.ShowDialog();
    string taskStatus = loadingBar.TaskStatus;
    if(taskStatus == "Cancelled")
    {
        int num2 = (int)bunifuSnackbar1.Show(this, "Backup cancelled", Bunifu.UI.WinForms.BunifuSnackbar.MessageTypes.Error, 4000);

    }
    else if(taskStatus =="")
    {
        txtBackupFilePath.Text = Path.GetDirectoryName(saveFileDialog.FileName);
        txtBackupFileName.Text = Path.GetFileName(saveFileDialog.FileName);
        int num3 = (int)bunifuSnackbar1.Show(this, "Backup generated successfully", Bunifu.UI.WinForms.BunifuSnackbar.MessageTypes.Error, 4000);
    }
    else
    {
        int num4 = (int)bunifuSnackbar1.Show(this, taskStatus, Bunifu.UI.WinForms.BunifuSnackbar.MessageTypes.Information, 4000);

    }
}

 Form Code 1

private void LoadingBar_Load(object sender, EventArgs e)
{
    try
    {
        bgWorkerThread.RunWorkerAsync((object)parameterValues);
    }
    catch(Exception ex)
    {
        TaskStatus = ex.Message;
    }
}

Form Code 2 LoadgingBar 

private void BgWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    lblStatus.Text = e.UserState.ToString();
    if (e.ProgressPercentage >= 0)
    {
        progressBarStatus.BorderStyle = BorderStyle.None;
        progressBarStatus.Value = e.ProgressPercentage;
    }
    else
        progressBarStatus.BorderStyle = BorderStyle.None;
}

Form Code 2 LoadgingBar 

private void BgWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    TaskStatus = !e.Cancelled ? (e.Error == null ? "" : e.Error.ToString()) : "Cancelled";
    Close();
}

private void BtnCancel_Click(object sender, EventArgs e) => bgWorkerThread.CancelAsync();

 


Answers (1)