report progress backgroundworker from different class c#

前端 未结 3 1236
星月不相逢
星月不相逢 2020-12-05 11:29

In my .NET C# project I have used a \"BackgroundWorker\" to call a method in a different class. The following is the source-code of my main form

public parti         


        
3条回答
  •  甜味超标
    2020-12-05 12:18

    I've just had this same issue (my long running process is a database restore), and solved it in a similar way by raising an event in the other class, but then had my subscriber to that event just act as a wrapper to backgroundWorker1.ReportProgress().

    private void DBRestoreProgressHandler(DataAccess da, DataAccess.DatabaseRestoreEventArgs e)
        {
            backgroundWorker1.ReportProgress(e.RestoreProgress);
        }
    
    private void backgroundWorker1_ReportProgress(object sender, ProgressChangedEventArgs e)
        {
            someLabel.Text = e.ProgressPercentage.ToString();
        }
    

    This saved having to use:

    base.Invoke((Action)delegate
    

    Which I think can then cause problems if the form closes unexpectedly?

提交回复
热议问题