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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:45:28+00:00 2026-05-26T13:45:28+00:00

All, I have successfully used ADO.NET to make use of asynchronous SQL queries similar

  • 0

All, I have successfully used ADO.NET to make use of asynchronous SQL queries similar to the example below. In the example shown the method ExecNonQuery is being invoked from the UI thread. This works well, but I wondered how I would handle the callback if I were to call ExecNonQuery from a non-UI thread?

Note. Clearly, in such a case I would amend ExecNonQuery, so that such things as this.toolStripStatusLabel1.Text were dealt with accordingly, or removed.

public bool ExecNonQuery(string strCmd, string strUserMsg = "")
{
    try
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = strCmd;
        cmd.CommandTimeout = 0;
        bIsExecuting = true;
        AsyncCallback callback = new AsyncCallback(HandleCallback);
        cmd.BeginExecuteNonQuery(callback, cmd); 
        return true;
    }
    catch (Exception Ex)
    {
        bIsExecuting = false;
        this.toolStripStatusLabel1.Text = String.Format("Ready (last error: {0})", Ex.Message);
        if (conn != null)
            conn.Close();
    }
    return false;
}

private delegate void DisplayInfoDelegate(string Text);

private void HandleCallback(IAsyncResult result)
{
    try
    {
        // Retrieve the original command object, passed
        // to this procedure in the AsyncState property
        // of the IAsyncResult parameter.

        SqlCommand command = (SqlCommand)result.AsyncState;
        int rowCount = command.EndExecuteNonQuery(result);
        string rowText = " rows affected.";
        if (rowCount == 1)
            rowText = " row affected.";
        rowText = rowCount + rowText;

        // Call the procedure from the form's thread.
        DisplayInfoDelegate del = new DisplayInfoDelegate(DisplayResults);
        this.Invoke(del, rowText);
    }
    catch (Exception ex)
    {
        // Because you are now running code in a separate thread, 
        // if you do not handle the exception here, none of your other
        // code catches the exception.

        // You can create the delegate instance as you 
        // invoke it, like this:
        this.Invoke(new DisplayInfoDelegate(DisplayResults), 
            String.Format("Ready(last error: {0}", ex.Message));
    }
    finally
    {
        bIsExecuting = false;
        if (conn != null)
            conn.Close();
    }
}

private void DisplayResults(string Text)
{
    this.toolStripStatusLabel1.Text = Text;
    this.toolStripProgressBar1.Style = ProgressBarStyle.Blocks;
    this.toolStripProgressBar1.Value = 100;
}

Thanks for you time.

  • 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-26T13:45:29+00:00Added an answer on May 26, 2026 at 1:45 pm

    It makes no difference to your callback which thread runs ExecNonQuery – HandleCallback will still be run on a thread pool thread.

    You have already spotted the change you need to make: don’t access UI controls directly in ExecNonQuery if it is not being run on the UI thread.

    Nick

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

Sidebar

Related Questions

Is the TransactionScope can be used only for Database/ADO.NET related methods? Say, I have
Is it possible to conform to UIApplicationDelegate and successfully have all of the functions
I have successfully migrated all my databases from 2008 to 2012 and mapped the
All these years i have been successfully using EnumServicesStatus in combination with OpenScManager (with
I have created many TestProjects with Visual-Studio-2010 Ultimate . All tests were executed successfully
I have successfully used the following command line to create a new GIF image
I have successfully used ajax to refresh in-line, via ajax, a contact form. I'm
I have a bit of javascript that I have used successfully on one page,
I have a bit of javascript that I have used successfully on one page,
We have a pretty cool little web framework that we have used successfully on

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.