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\$
  • \$\begingroup\$ Have you considered async instead? \$\endgroup\$ – BCdotWEB Feb 15 '16 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 Feb 15 '16 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.