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

  • SEARCH
  • Home
  • 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 6533907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:11:12+00:00 2026-05-25T10:11:12+00:00

I am noticing a pattern like below in few of my classes. How can

  • 0

I am noticing a pattern like below in few of my classes.
How can I abstract that away? Any suggestions?

private void InitializeClass()
{
    BackgroundWorker bgw1 = new BackgroundWorker();
    bgw1.DoWork += (s,e) =>
    {
        // doing work
    };

    BackgroundWorker bgw2 = new BackgroundWorker();
    bgw2.DoWork += (s,e) =>
    {
        // doing work
    };

    BackgroundWorker bgw3 = new BackgroundWorker();
    bgw3.DoWork += (s,e) =>
    {
        // doing work
    };

    bgw1.RunWorkerAsync();
    bgw2.RunWorkerAsync();
    bgw3.RunWorkerAsync();
}
  • 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-05-25T10:11:12+00:00Added an answer on May 25, 2026 at 10:11 am
    public static class Worker
    {
        public static void Execute(params DoWorkEventHandler[] handlers)
        {
            foreach (DoWorkEventHandler handler in handlers)
            {
                BackgroundWorker worker = new BackgroundWorker();
                DoWorkEventHandler capturedHandler = handler;
    
                worker.DoWork += (sender, e) =>
                {
                    try
                    {
                        capturedHandler(sender, e);
                    }
                    finally
                    {
                        worker.Dispose();    
                    }
                };
    
                worker.RunWorkerAsync();
            }
        }
    }
    

    and then:

    Worker.Execute((s, e) =>
    {
        // doing work
    });
    

    or if you wanted to schedule multiple events:

    Worker.Execute(
        (s, e) => 
        {
            // doing work
        },
        (s, e) => 
        {
            // doing work
        },
        (s, e) => 
        {
            // doing work
        }
    );
    

    UPDATE:

    Here’s an alternative which allows you to specify a completed handler:

    public class Worker
    {
        public Worker Work(DoWorkEventHandler doWork, RunWorkerCompletedEventHandler complete)
        {
            var worker = new BackgroundWorker();
            worker.DoWork += doWork;
            worker.RunWorkerCompleted += complete; 
            worker.RunWorkerAsync();
            return this;
        }
    }
    

    and then:

    new Worker()
        .Work((s, e) => { /** some work **/ }, (s, e) => { /** work completed **/ })
        .Work((s, e) => { /** some work **/ }, (s, e) => { /** work completed **/ })
        .Work((s, e) => { /** some work **/ }, (s, e) => { /** work completed **/ });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm noticing a problem that has crept up a few times over the years,
I'm noticing some behavior that I don't like, and I'm wondering if this is
I'm noticing that StrictMode is complaining quite loudly about WebView creation. Is there any
I've been noticing answers on stack overflow that use terms like these, but I
I have been noticing, over the last few weeks, that my installation of VS2008
I'm noticing that, if the ThreadPool max thread count for my IO-intensive app is
I have been noticing on my trackers that bots are visiting my site ALOT.
I have been noticing __construct a lot with classes. I did a little reading
I'm noticing that some of the things that I do with images in my
I'm interested in learning more about pattern recognition. I know that's somewhat of a

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.