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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:46:16+00:00 2026-06-13T09:46:16+00:00

In following code snippet, AddRow() is called from a non-UI thread: public partial class

  • 0

In following code snippet, AddRow() is called from a non-UI thread:

  public partial class Form1 : Form
  {
    public delegate void InvokeDelegate();

    ...
    SqlConnection mSqlConnection = new SqlConnection("Data Source=" + Environment.MachineName + "\\SQLEXPRESS; Initial Catalog=orderDB; Integrated Security=TRUE; MultipleActiveResultSets=True;");

    DataSet mDataSet = new DataSet();
    SqlDataAdapter mSqlDataAdapter = new SqlDataAdapter();


    ...
    private void UpdateGridView()
    {
      if (mSqlConnection.State == ConnectionState.Closed)
        mSqlConnection.Open();

      mSqlDataAdapter.SelectCommand = new SqlCommand("SELECT * FROM customerTable", mSqlConnection);
      mDataSet.Clear();
      mSqlDataAdapter.Fill(mDataSet);
      dataGridView1.DataSource = mDataSet.Tables[0];

      if (mSqlConnection.State == ConnectionState.Open)
        mSqlConnection.Close();
    }


    public void AddRow(int field1, int field2, int field3)
    {
      mSqlDataAdapter.InsertCommand = new SqlCommand("INSERT INTO customerTable VALUES(@field1, @field2, @field3)", mSqlConnection);

      mSqlDataAdapter.InsertCommand.Parameters.Add("@field1", SqlDbType.Int).Value = field1;
      mSqlDataAdapter.InsertCommand.Parameters.Add("@field2", SqlDbType.Int).Value = field2;
      mSqlDataAdapter.InsertCommand.Parameters.Add("@field3", SqlDbType.Int).Value = field3;

      mSqlConnection.Open();
      mSqlDataAdapter.InsertCommand.ExecuteNonQuery();
      dataGridView1.BeginInvoke(new InvokeDelegate(UpdateGridView)); // UpdateGridView() won't work from a non-UI thread
      mSqlConnection.Close();

    }
}

Before having to call AddRow() from a non-UI thread, I had UpdateGridView() being called directly and it worked flawlessly. But now AddRow() is no longer guaranteed to be called from a UI thread and so I replaced the direct call with dataGridView1.BeginInvoke().

As soon as I did that, my form-based application started throwing a System.InvalidOperationException every several AddRow() calls, breaking on the mSqlDataAdapter.Fill(mDataSet); statement (!) with the following message:

Invalid attempt to call Read when reader is closed

My question is why?

  1. What reader? the DataAdapter’s? The SqlConnection’s? The DataGridView’s data source?
  2. I am taking care of surrounding BeginInvoke() with mSqlConnection’s Open() and Close() and I even open mSqlConnection (again!) if it isn’t open, so how come I’m receiving this “closed” error?
  3. What is the correct way to solve this problem? (i.e. updating DataGridView from a non-UI thread)
  • 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-13T09:46:16+00:00Added an answer on June 13, 2026 at 9:46 am

    The issue is certainly due to the race condition.

    Remove these two lines from UpdateGridView,as it is not the right place to close your connection.

     if (mSqlConnection.State == ConnectionState.Open)
        mSqlConnection.Close();
    

    use IAsyncResult to retrieve a waithandle and wait for the thread to complete the GridUpdate.

     IAsyncResult Result = dataGridView1.BeginInvoke(new InvokeDelegate(UpdateGridView));
     Result.AsyncWaitHandle.WaitOne();
     mSqlConnection.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code snippet will result in a run-time: class Vehicle { public void
The following code snippet is from book Effective C#, public event AddMessageEventHandler Log; public
The following code snippet works for me: class Foo { public: template <class T>
I am getting the following code snippet from Android's Pair.java public boolean equals(Object o)
I saw the following code snippet: class WindowHandle { public: WindowHandle(WINDOW_HANDLE handle) : w(handle)
The following code snippet is from ocsigen source. I donot understand int_of_string called with
The following code snippet is from The Official GNOME 2 Developer's Guide : GMemChunk
The following code snippet is taken from android JellyBean ReferenceQueue.java in the libcore project.
the following code snippet taken from http://perldoc.perl.org/perlrequick.html#Search-and-replace gives me Bareword found where operator expected
I have the following code snippet, which loads data from a CSV file into

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.