Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:21:48+00:00 2026-06-04T02:21:48+00:00

In my application’s Business Logic layer I have the following classes: public class EocMonitor

  • 0

In my application’s Business Logic layer I have the following classes:

public class EocMonitor : DeviceMonitor {

    public BackgroundWorker BackendWorker { get; set; }

    public BackgroundWorker EocWorker { get; set; }

    public EocMonitor() {
        BackendWorker = new BackgroundWorker {
            WorkerReportsProgress = true,
            WorkerSupportsCancellation = true
        };
        BackendWorker.DoWork += BackendWorker_DoWork;

        EocWorker = new BackgroundWorker {
            WorkerReportsProgress = true,
            WorkerSupportsCancellation = true
        };
        EocWorker.DoWork += EocWorker_DoWork;
    }

    private void BackendWorker_DoWork( object sender, DoWorkEventArgs e ) {
        // Does some lengthy operation
    }

    void EocWorker_DoWork( object sender, DoWorkEventArgs e ) {
        // Does some lengthy operation
    }

    public void GetDiagnostics() {
        BackendWorker.RunWorkerAsync( new DiagnosticsInfo() );
            EocWorker.RunWorkerAsync( new DiagnosticsInfo() );
    }

}

public class DiagnosticsInfo {

    public int DataTypeCount { get; set; }

    public int DataTypesProcessed { get; set; }
}

The BackgroundWorkers are used to query information over the wire from 2 other processes running in my application. The responses can take a while to come back. Plus the data can take a while to come back.

I have a WPF UserControl in my application’s main window called Dashboard. The Dashboard has a DataGrid on it that displays the results of the lengthy operations. Because they are lengthy, it also has a Button on it called Refresh that starts the process off. And, because it can take a long time to run, there’s a UserControl I wrote called a ProgressControl on the form. This consists of a Cancel Button, a ProgressBar, and a TextBlock where messages can be displayed. When the user clicks on the Cancel Button, the refresh stops.

Here’s some code from Dashboard:

public partial class Dashboard : UserControl {

    public Dashboard() {
        InitializeComponent();
    }

    private Dashboard_Loaded( object sender, RoutedEventArgs e ) {
        if ( !setupProgress && EocMonitor != null ) {
             EocMonitor.BackendWorker.ProgressChanged += BackendWorker_ProgressChanged;
             EocMonitor.BAckendWorker.RunWorkerCompleted += BackendWorker_RunWorkerCompleted;
             EocMonitor.EocWorker.ProgressChkanged += EocWorker_ProgresChanged;
             EocMonitor.EocWorker.RunWorkerCompleted += EocWorker_RunWorkerCompleted;
        }
    }

    private void BackendWorker_ProgressChanged( object sender, ProgressChangedEventArgs e ) {
        DiagnosticsInfo info = e.UserState as DiagnosticsInfo;

        // Other processing to notify the user of the progress
    }

    private void BackendWorker_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e ) {
        // Processing to do when the back-ground worker is finished
    }

    private void DiagnosticsProgressCtrl_Click( object sender, RoutedEventArgs e ) {
        EocMonitor.BackendWorker.CancelAsync();
        EocMonitor.    EocWorker.CancelAsync();

        DiagnosticsProgressCtrl.Visibility = Visibility.Collapsed;

        e.Handled = true;
    }

    void EocWorker_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e ) {
        // Processing to do when the back-ground worker is finished
    }

    private void RefreshButton_Click( object sender, RoutedEventArgs e ) {
        DiagnosticsProgressCtrl.Maximum = DiagnosticsProgressCtrl.Minimum = DiagnosticsProgressCtrl.Value = 0.0;

        DiagnosticsProgressCtrl.Visibility = Visibility.Visible;

        backendDataTypeCount   = eocDataTypeCount   = 0;
        backendWorkerCompleted = eocWorkerCompleted = false;

        EocMonitor.GetDiagnostics();

        e.Handled = true;
    }

}

The problem is that I have placed breakpoints in the DoWork methods and watched them run to completion, yet the RunWorkerCompleted methods are not being called. No errors are occurring or being thrown. This thing is the EocMonitor class and the Dashboard class are in two different DLLs. Does that make a difference? As far as I know it shouldn’t, but I don’t understand why the completed event handlers aren’t getting called. Should I instantiate the BackgroundWorkers in the front-end application?

Tony

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:21:49+00:00Added an answer on June 4, 2026 at 2:21 am

    Well, after I posted the above, I went back and changed things a bit. I now instantiate the BackgroundWorker objects in the Dashboard control and pass them to the EocMonitor’s GetDiagnostics method. The properties in EocMonitor that hold these objects have private setters, so the only way to use them is to create them & pass them to that method. The code in the Dashboard_Loaded is now moved in the RefreshButton_Click method and runs after the objects are instantiated, before they’re passed to GetDiagnostics.

    This all works now! I see the Progress_Changed methods and the RunWorkerCompleted methods run.

    It just hit me why it’s probably not working. The EocMonitor object is created on a non UI thread during my program’s initalization phase. Since it’s calling methods in a UI object, the methods probably can’t be called. An Invalid operation exception of some sort is probably being thrown, but there’s no place to catch it.

    So let that be a lesson: The BackgroundWorker has to be instantiated in code on the UI thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The application I'm currently writing is using MVVM with the ViewModel-first pattern. I have
Application uses Entity Framework 4.1 with database first approach. I have in database a
application design/architecture question for a project that I am building. I have a main
My application is having two activities. In first activity, I have one button and
Application use NHibernate. I Have object A that contains set of objects B. I
My application was running fine until i added the following code to add an
Application has many extension assemblies and they contain mappings for their classes. I need
Application has 7-8 activities, so I have create an application with some background music
Application I am working on an MMO and have run into an issue. The
Application able to record error in OnError, but we are not able to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.