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 8620541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:36:27+00:00 2026-06-12T06:36:27+00:00

Questions: What is the best way to check if two BackgroundWorkers returned true as

  • 0

Questions:
What is the best way to check if two BackgroundWorkers returned true as there values
or if neither returned true, or only one did.

Additional Info:

I have two BackgroundWorker currently checking to see if two SQL connections are
valid and returning a value depending whether a connection was made successfully.

The code is as follows:

private void btnTestSConnection_Click(object sender, EventArgs e)
        {
            BackgroundWorker work1 = new BackgroundWorker { WorkerSupportsCancellation = true };
            BackgroundWorker work2 = new BackgroundWorker { WorkerSupportsCancellation = true };
            work1.RunWorkerCompleted += (item, a) =>
            {
                //need to figure out this portion
            };
            work2.RunWorkerCompleted += (item, a) =>
            {
                //need to figure out this portion
            };

            work1.DoWork += doWork;
            work2.DoWork += doWork;

            SourceString.InitialCatalog = txtSSourceDatabase.Text;
            work1.RunWorkerAsync(SourceString.ConnectionString);
            SourceString.InitialCatalog = txtSSystemDatabase.Text;
            work2.RunWorkerAsync(SourceString.ConnectionString);
        }

DoWorkEventHandler doWork = (sender, e) =>
        {
            SqlConnection Connection;
            BackgroundWorker worker = sender as BackgroundWorker;
            for (int i = 1; (i <= 10); i++)
            {
                    try
                    {
                        using (Connection = new SqlConnection((string)e.Argument))
                        {
                            Connection.Open();
                        }
                        e.Result = true;
                    }
                    catch (SqlException c)
                    {
                        e.Result = false;
                    }
                }
        };
  • 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-12T06:36:28+00:00Added an answer on June 12, 2026 at 6:36 am

    You may returned a KeyValuePair where the first bool represent the which worker were used (true for work1, false for work2) and the second bool is the returned value of the DoWork method like this:

    work1.DoWork += doWork;
    work2.DoWork += doWork;
    
    work1.RunWorkerAsync(true);
    work2.RunWorkerAsync(false);
    
    private void doWork(s, e)
    {
      var kvp = new KeyValuePair<bool, bool>;
      kvp.Key = e.Argument as bool; // this indicate which of the worker returned a value
      ...
      using (Connection = new SqlConnection((string)e.Argument))
      {
        Connection.Open();
      }
      kvp.Value = true; // this is the result of your connection test
      ...
      e.Result = kvp
    };
    

    Now on your RunWorkerCompleted, you can cast the Result to a KeyValuePair and get if weither the work1 or work2 returned a which value.

    b.RunWorkerCompleted += (item, a) =>
    {
      var kvp = a.Result as KeyValuePair<bool, bool>;
      //kvp.Key == true mean this is the work1
      //kvp.Value is the SQL connection test
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Three SHA512Managed related questions: Is SHA512Managed considered the best one-way hash available in .NET
This question is related to this one: Best way to store product colors in
This question ( Best way to strip punctuation from a string in Python )
I found another similar question: Best way to download images from XML content in
Question: What is the best way to set focus to a web control in
My question is: What is the best way to port the data back and
General question about java servlets and the best way to handle requests. If I
I think the best way to express my question is by showing an example:
This is the best way I can think of phrasing this question, given this
Another newbie question: What's the best way to store data in a Cocoa application

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.