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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:44:26+00:00 2026-05-22T19:44:26+00:00

This is a multi threaded scenario. The main thread handles the application and UI

  • 0

This is a multi threaded scenario.

  1. The main thread handles the application and UI events, and it starts up a new thread to do some background operations.
  2. The “background” thread loads the data from files into a data-table of a strongly-typed dataset. The DataGridView is bound to that DataTable.
  3. Once the data is ready, the “background” thread invokes the refresh() function of the DataGridView on the form.

If there are more lines then what fits on one screen and the vertical scrollbar is to appear: the grid crashes. The new datalines are always displayed. Error only occurs if there are enough lines to display the scrollbar (see image below).

Sample image of DataGridView crash

I use .NET 3.5. In Windows XP it crashes the whole application. On Win 7 (64 bit) only the grid becomes unresponsive, but once I resize the window the scrollbar appears and all is fine.

The relevant parts of the code are attached below.

Grid refresh operation in the form’s .cs file:

    public void ThreadSafeRebindGrids()
    {
        SimpleCallBack callBackHandler = new SimpleCallBack(RebindGrids);
        this.BeginInvoke(callBackHandler);
    }
    public void RebindGrids()
    {
        gridCurrentResults.Refresh(); // The problematic DataGridView refresh()
        gridAllResults.Refresh();
    }
    public delegate void SimpleCallBack();

The update part in the “background” thread:

    void Maestro32_SampleFinished(object sender, MeasurementEvents.SampleFinishedEventArgs e)
    {
        //--- Read new results
        ParentForm.ThreadSafeSetStatusInfo("Processing results for sample no. " + e.SampleNo.ToString() + "...");
        CurrentMeasurement.ReadSpeResults();   // Updating the DataTable in the strongly typed DataSet (see below)
        ParentForm.ThreadSafeRebindGrids();    // Refresh the DataGridView
        ParentForm.ThreadSafeRefreshNumbers();
    }

The objects related to the “background” thread have a direct reference to the DataSet (UiDataSource). The DataTable (CurrentSamples) is updated in the following manner:

    /// <summary>
    /// Adds a new sample to the CurrentSamples table of the UiDataSet.
    /// </summary>
    /// <param name="sample">The new sample to be added to the table.</param>
    /// <param name="serial">The serial number of the sample being added</param>
    private void AddSampleToCurrentResults(SampleData sample, int serial)
    {
        UiDataSource.CurrentSamples.AddCurrentSamplesRow(serial,
                                                       sample.MeasurementDate,
                                                       (uint)Math.Round(sample.SampleCountSum),
                                                       true, //--- Set the checkbox checked
                                                       sample.LiveTime,
                                                       sample.RealTime);
    }

DataGridView options:

        // 
        // gridCurrentResults (generated)
        // 
        this.gridCurrentResults.AllowUserToAddRows = false;
        this.gridCurrentResults.AllowUserToDeleteRows = false;
        this.gridCurrentResults.AllowUserToOrderColumns = true;
        this.gridCurrentResults.AllowUserToResizeRows = false;
        this.gridCurrentResults.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.gridCurrentResults.AutoGenerateColumns = false;
        this.gridCurrentResults.CausesValidation = false;
        this.gridCurrentResults.ColumnHeadersHeight = 25;
        this.gridCurrentResults.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.selectedCol,
        this.SampleNoCol,
        this.MeasuredValueCol,
        this.liveTimeCol,
        this.realTimeDataGridViewTextBoxColumn,
        this.AtTimeCol});
        this.gridCurrentResults.DataMember = "CurrentSamples";
        this.gridCurrentResults.DataSource = this.uiDataSource;
        this.gridCurrentResults.Location = new System.Drawing.Point(11, 24);
        this.gridCurrentResults.Margin = new System.Windows.Forms.Padding(8);
        this.gridCurrentResults.Name = "gridCurrentResults";
        this.gridCurrentResults.RowHeadersVisible = false;
        this.gridCurrentResults.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        this.gridCurrentResults.ShowEditingIcon = false;
        this.gridCurrentResults.Size = new System.Drawing.Size(534, 264);
        this.gridCurrentResults.TabIndex = 0;
        this.gridCurrentResults.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.gridCurrentResults_CellContentClick);

If I made a mistake somewhere please point it out to me.

@ChrisF:

I tried removing the refresh() statement, as I am doing pretty much the same what u suggested. The only difference is the databinding, it looks like:

this.dataGridView.DataSource = this.dataSet;
this.dataGridView.DataMember = "dataTable";

And I update the dataTable in a similar way, but from another thread.

But the new data lines do not appear until I, say, resize the window.

Which raises the question how I can properly update the dataTable from another 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-05-22T19:44:27+00:00Added an answer on May 22, 2026 at 7:44 pm

    I’m guessing the problem has to do with how WinForms works inside the STA model for threading. Basically, the DataTable you’re accessing is located somewhere, and that is probably inside the form we see above. So, when you update the DataTable from another thread, which thread gets the events needed for binding? Likely the thread you update it from, and the form’s thread is not aware of the changes being made. So, you simply need to invoke any calls to DataTable onto the form itself, so it receives the events properly:

    this.Invoke(() => {
       // any calls involving DataTable
    });
    

    It seems backwards, but keep in mind in an “enterprise” situation, you’d probably be accessing that dataset by multiple adapters. So, your update thread would have an adapter to itself, and your GUI would have its own also. The other solution would be to use a BindingList, which I believe has thread compatibility for this type of situation, but don’t quote me on that.

    For extra credit, this could also explain your problem before with crashing. By accessing the DataGridView from the background thread, you had cross-thread operations going on.

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

Sidebar

Related Questions

I am creating a multi-threaded application. However, I have experienced lots of unexpected behavior
I have a fairly complex multi threaded application (server) that from time to time
In a C++ multi-threaded application with many classes, i am trying to find out
I have a CPU intensive multi-threaded Java application and I'm looking for ways to
I have a multi-threaded application build in C# using VS2010 Professional. It's quite a
Im trying to apply the state pattern on a multi threaded application.The problem is
Normally it is said that multi threaded programs are non-deterministic, meaning that if it
I'm trying to implement a multi threaded, recursive file search logic in Visual C++.
Can I unit test my multi-threaded code using CHESS & MSTest in VS 2010.
I'm modifying an application written in C# that makes heavy-use of multi-threading to play

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.