2
\$\begingroup\$

I've implemented a BackgroundWorker in my WPF app. My code is pretty ugly though. I've been told to write as little as redundant code as possible. IS there some generic Method I could employ to reduce the amount of code?

    //BackgroundWorker Databases
    private void worker_updateDatabases(object sender, RunWorkerCompletedEventArgs e)
    {
        this.Databases = ModelSource.Databases;
        this.IsLoading = false;
    }

    private void worker_reportDatabaseProgress(object sender, ProgressChangedEventArgs e)
    {
        this.IsLoading = true; 
    }

    private void worker_FetchDatabases(object sender, DoWorkEventArgs e)
    {            
        ModelSource.getDatabasesAsync(this._currentConfig);
        (sender as BackgroundWorker).ReportProgress(ModelSource.Databases.Count);
    }

    //BackgroundWorker Tables
    private void worker_updateTables(object sender, RunWorkerCompletedEventArgs e)
    {
        this.Tables = ModelSource.Tables;
        this.IsLoading = false;
    }

    private void worker_reportTableProgress(object sender, ProgressChangedEventArgs e)
    {
        this.IsLoading = true;
    }

    private void worker_FetchTables(object sender, DoWorkEventArgs e)
    {

        ModelSource.getTablesAsync(SelectedDatabase.Name);
        (sender as BackgroundWorker).ReportProgress(ModelSource.Tables.Count);
    }
    #endregion
\$\endgroup\$
2
  • \$\begingroup\$ Have you considered async instead? \$\endgroup\$
    – BCdotWEB
    Commented Feb 15, 2016 at 13:24
  • \$\begingroup\$ Yep. Problem is, I'm a beginner to WPF, and only an advanced beginner to C#. Investing time into learning TPL is not an option due to time constraints (not due to laziness). \$\endgroup\$
    – Fang
    Commented Feb 15, 2016 at 13:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.