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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:39:28+00:00 2026-06-12T06:39:28+00:00

I have a BindingList databound to a datgridview. I’m using it to keep track

  • 0

I have a BindingList databound to a datgridview. I’m using it to keep track of some real-time prices. The method ‘update(Quote quote)’ is called multiple times a second by various threads. If the datagridview doesn’t contain the Quote, it is added. If it does, the values of the quote are updated. I don’t want the same quote to appear in the BindingList (or on the GUI) twice, so I tried to put a lock around the operation that checks whether the value is in the list or not. It doesn’t work! What am I doing wrong? I’ve tried two different ways of locking, and am locking on a String object rather than just an object. The problem is definitely in the BeginInvoke(new MethodInvoker(delegate() { activeQuotes.Insert(0, quote); })); call (which is probably taking some time), but if I make that synchronous, the ‘add’ method throws a ‘cross-threading’ error. . . What can I do to avoid the cross-threading error, but ensure that the lock works also??

public BindingList<Quote> activeQuotes = new BindingList<Quote>();
object lockObject = "lockObject";

dataGridViewActive.DataSource = activeQuotes;


public void update(Quote quote)
{
//lock (lockObject)
if(Monitor.TryEnter(lockObject))
{
try
   {
    if (!activeQuotes.Contains(quote))
    {
       try
       {
          activeQuotes.Add(quote);
          AddQuote(quote);
       }
       catch (Exception ex)
       {
          Console.WriteLine("Datagridview!!!!!!");
       }
     }
 else
 {
   int index = activeQuotes.IndexOf(quote);
   activeQuotes[index].Bid = quote.Bid;
   activeQuotes[index].Ask = quote.Ask;
   activeQuotes[index].Mid = quote.Mid;
   activeQuotes[index].Spread = quote.Spread;
   activeQuotes[index].Timestamp = quote.Timestamp;
}
finally
{
    Monitor.Exit(lockObject);
}
}

private void AddQuote(Quote quote)
{
     if (this.InvokeRequired)
     {
                BeginInvoke(new MethodInvoker(delegate() { activeQuotes.Insert(0, quote); }));
                BeginInvoke(new MethodInvoker(delegate() { dataGridViewActive.Refresh(); }));
                BeginInvoke(new MethodInvoker(delegate() { dataGridViewActive.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); }));
    }
    else
    {
                activeQuotes.Add(quote);
                dataGridViewActive.Refresh();
                dataGridViewActive.AutoResizeColumns (DataGridViewAutoSizeColumnsMode.AllCells);
     }
}

I’d appreciate any help at all on this.

Thanks.

  • 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-12T06:39:29+00:00Added an answer on June 12, 2026 at 6:39 am

    I think you should change your BeginInvoke to just Invoke. You need to get it on the UI thread, not begin an async operation. Otherwise your lock could get released before the BeginInvoke target gets invoked because control is returned immediately upon calling BeginInvoke. Calling Invoke will block that thread on that call until the Invoke target completes then return control back to your thread, which will ensure the lock is kept.

    Also, have you considered using a lock block instead of Monitor method calls? It’s basically the same thing but prevents you from needing the try/finally. I don’t see that you’re using any retry or benefit from the TryEnter, but perhaps the code sample doesn’t demonstrate that.

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

Sidebar

Related Questions

I have overridden the ApplySortCore method for a custom BindingList like so: public void
I have a BindingList with objects in which is saved some contacts. internal static
I have a datasource (BindingList) with many Users, but there are some users I
I have a bindinglist. i am using a textbox to show its items WithEvents
So, I have a DataGridView using as datasource a BindingList DataGridView.DataSource = new BindingList<Car>{...}
I have a BindingList<> of a class set to the DataSource property of a
I have a DataGridView populated with a BindingList . This list gets saved into
I have got: var orderedStatItmes = item.Statistics.OrderByDescending(t=>t.StartTime); Note that public BindingList<IStatistics> Statistics { set;
I have a class, say DerivedBindingList<T> , which is derived from BindingList<T> . I
have written this little class, which generates a UUID every time an object of

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.