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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:28:33+00:00 2026-06-08T14:28:33+00:00

I have a WPF application where the user enters database information in some textboxes.

  • 0

I have a WPF application where the user enters database information in some textboxes. Once the user clicks “connect”, a connection string is created from what the user had entered and a connection is established. I noticed that if the user enters any info that is wrong, the application will hang until the connection times out. By hang, I mean the user can’t interact with the rest of the application at all.

It is my goal to keep the application responsive while the connection string is tested.

I thought that putting this workflow on a different thread is a good solution. My idea is to just disable anything that may need a database connection while the thread runs. Once the thread comes back (and has has confirmed the connection string to be valid) I would re-enable everything. Otherwise, leave everything disabled.

However, the Thread class doesn’t have an event notification when the thread is done (or at least I am unaware of one).

I have also worked with the BackgroundWorker class. This works better. However, when the RunWorkerCompletedEventHandler event is fired and the connection string isn’t valid, I get the following exception:

The calling thread cannot access this object because a different
thread owns it.

This is probably because the connection still hasn’t timed out when the completed event handler is fired.

Does anybody have any ideas, or should I just NOT try to multithread a connection to a database?

A code outline of what I am doing:

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    dbTool = new DBTool();
    // Create the connection string
    e.Result = dbTool.connectToDB(); // connectToDB() returns a bool (true if connection established)
}

private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // connectToDB() returns a bool (true if connection established)
    if(e.Result == true)  // Trying to read e.Result here throws the exception
    {
        // e.Error and e.Cancel should be checked first
        // However, I would like the thread to finish before
        // this event is fired
    }
    if (e.Error != null)
    {
        Console.WriteLine(e.Error.Message);
    }
}
  • 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-08T14:28:34+00:00Added an answer on June 8, 2026 at 2:28 pm

    Thank you everyone for your input.

    I was able to come up with a solution. After coming across Working With The WPF Dispatcher. I determined that you can obtain the UI thread’s Dispatcher object:

    //...
    
    dbTool = new DBTool();
    // Initialize the connection string
    // Disable some UI
    Thread thread = new Thread(new ThreadStart(
            delegate()
            {
                dbTool.connectToDB();
                UIControl.Dispatcher.BeginInvoke(
                  new Action(
                      update
                ));
            }
    ));
    thread.Start();
    
    //.....
    
    void update()
    {
        if (dbTool.validString)     // If the connection string was valid
        {
            // Re-enable controls
        }
        else     // Invalid connection string
        {
            // Keep controls disabled if no connection could be created
        }
    }
    

    This indeed will test the connection string on a different thread, leaving the rest of the application responsive.

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

Sidebar

Related Questions

I have created a wpf application into which I have added a user control
I have a WPF application using MVVM. I have some user controls that show
All, I have a WPF application built upon SQLite database and user/application Configuration settings.
I have WPF Application where I have One main form and other user controls
I have a WPF application, where when a user unchecks a checkbox, the application
I have a service that spawns a WPF application process when a user logs
I have a fairly large application that uses WPF for its user interface. I
I have a WPF application which connects to a remote database over internet and
I have WPF Application. I want to give ability for user by checkbox to
I have a WPF application that contains some Window s with few UserControl s

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.