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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:25:48+00:00 2026-05-26T05:25:48+00:00

All, I have some complex C# code (Windows Forms) that is heavily embedded with

  • 0

All, I have some complex C# code (Windows Forms) that is heavily embedded with calls to SQL Server (2008 R2 or higher is the assumed server version). As it stands. the code is serial and I have been asked to multithread it. This process of multithreading is now specifically associated with the expensive parts of the code, i.e. where the code does “Heavy Lifting”. The majority of work is done with SQL Server queries.

I am just about to embark upon multithreading the main processor that deals with calls to SQL Server. I want to provide information to the user by-way of progress from the “SQL Thread” as the SQL procedures can be very long. I want to know if my method sounds reasonable or if there is a better way of doing it. My approach is as follows: (Note: the code below is just a small example I have built before attempting this with the actual code)

A. Start a BackgroundWorker thread from a button click event on the main form.

Bgw = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
Bgw.DoWork += new DoWorkEventHandler(Bgw_DoWork);
Bgw.ProgressChanged += new ProgressChangedEventHandler(Bgw_ProgressChanged);
Bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Bgw_RunWorkerCompleted);
Bgw.RunWorkerAsync();  

B. From the Bgw_DoWork event, I launch my SQL method which is located in class HeavyWork.

void Bgw_DoWorkSQL(object sender, DoWorkEventArgs e)
{
    Hw = new HeavyWork(this, ref Bgw, ref e);
    Hw.SQLProc();
    return;
} 

C. Start another background thread from the BackgroundWorker (this time not BackgroundWorker), from within Hw.SQLProc in order to capture progress of SQL queries and to facilitate the cancelation of the SQL Server query.

// Globals.
private bool bConnOpen = false;
private SqlConnection conn = null;
private Form _MainForm; 
private BackgroundWorker _Bgw;
private DoWorkEventArgs _e;    

public void SQLProc()
{
    bool bConnOpen = false;
    const string strSqlConnMaster = "Data Source = localhost; Initial Catalog = RMH1006DHFinal; Integrated Security " + "= True; MultipleActiveResultSets = True; Connection Timeout = 0";

    const string strSQL = "DBCC CHECKDB"; // Expensive SQL Non-Query.

    try
    {
        // Create new SQL connection.
        conn = new SqlConnection(strSqlConnMaster);

        // Execute the SQL Non-Query.
        conn.Open();
        bConnOpen = true;

        // Start another thread to get user information and for cancellation purposes.
        Thread SQLThread = new Thread(myMethod); // This is not working.
        SQLThread.IsBackground = true;
        SQLThread.Start();           

        // Now run big query.
        _Bgw.ReportProgress(0, String.Format("Processing SQL Command '{0}'...", strSQL));
        ExecNonQuery(conn, strSQL);
        conn.Close();
        bConnOpen = false;

        return;
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        if (bConnOpen)
            conn.Close();
    }
}

In summary I want to launch the expensive SQL query on a seperate BackgroundWorker. Within the method called on the BackgroundWorker thread (some query sent using an SQL connection, call it SqlConnection conn), launch another Thread which goes into another mathod using a seperate SqlConnection connNew which retreves the information about the major process on conn.

I would like to know is this a valid way of doing this? Also, if it is possible to launch new Threads from a BackGroundWorker as the code above is not starting the myMethod method on the new thread?

Thank you all in advance.

  • 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-26T05:25:48+00:00Added an answer on May 26, 2026 at 5:25 am

    If I understand correctly, you want to indicate progress, but can’t because ExecNonQuery blocks.

    As a matter of good practice, I would not use a BackgroundWorker for this if your db call might take more than say a second, as BW uses a thread pool thread. Instead, I would use a new Thread or, if you’re on .NET 4.0, a Task with TaskCreationOptions.LongRunning.

    Back to your progress reporting: your db call blocks, so the only progress you can show is running or completed, so I would show some sort of marquee progress indicator. I would control this from the UI thread using a Timer – you don’t really want to spin up a thread just to do an animation and you want to be on the UI thread anyway.

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

Sidebar

Related Questions

Hej All I have some code that builds a new TYPE runtime, it sets
I am creating a series of UserControls that all have some similar business logic.
I have some special parameters to all my wcf service methods that are handled
I have some functionality that I need in all my classes which derive from
I have a large old program which has some rather complex graphical displays (all
I have some Java code that validates XML against an XSD. I am using
I have some fairly complex libraries that interconnect with each other to do some
I have some fundamental points/questions about OpenGL, not all involving code but concepts as
I have some tables. These tables all have one column in common called 'classified_id':
How do you determine if all hash keys have some value ?

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.