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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:45:35+00:00 2026-05-25T21:45:35+00:00

I have a form with a control on it to display some custom objects.

  • 0

I have a form with a control on it to display some custom objects. In the form I subscribe to an event AddObject that adds objects to a ToAdd List as they come in from the server. I setup a timer to run every 10 seconds to copy the objects from the ToAdd List to the Display List (it was more efficient adding the items in bulk to the control than 1 at a time as they came in), which is bound to a control on my form, then I clear the ToAdd List. Is it safe to have the lock inside of the BeginInvoke? Is there a better way of doing this?

        private System.Threading.Timer aTimer;
        private readonly Object sync = new Object();
        List<object> ToAdd = new List<object();
        List<object> Display = new List<object();

        private void Init()
        {
           TimerCallback tcb = IntermittentProcessMessages;
           aTimer = new System.Threading.Timer(tcb, null, 1000, 100);
           Server.MessageReceived += AddObject;   
        }

        private void AddObject(object t)
        {
            lock (sync)
            {
                try
                {
                   ToAdd.Add(t);
                }
                finally() {}
            }
         }


        private void IntermittentProcessMessages(object source)
        {
            try
            {
                if (this.IsHandleCreated == false)
                {
                    return;
                }
                this.BeginInvoke((Action)delegate()
                {
                   lock (sync)
                   {
                       if (ToAdd.Count > 0)
                       {
                           ToAdd.ForEach(f => Display.Add(f));
                           ToAdd.Clear();
                       }
                   }
                }
           }
           finally(){}
    }
  • 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-25T21:45:35+00:00Added an answer on May 25, 2026 at 9:45 pm

    Yes it is safe. Technically the lock is not in the BeginInvoke, but in the anonymous function that is is created from the delegate.

    Some notes:

    • List<T> has an AddRange method that is more efficient than multiple Add.
      Use it like Display.AddRange(ToAdd);
    • The delegate in IntermittentProcessMessages is not covered by the try-catch since the BeginInvoke returns immediately.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that contains a GridView control which is databound to an
We have a form that hosts the WebBrowser control. That is the only control
I have customer that uses older, custom built ERP application for which they don't
We have some html that looks like this: <form id=MyUserControl runat=server method=post> <script language=javascript
Currently I have a custom user control and within it is a little form
I have a requirement to create a windows form control which has to detect
I have a form which has only tab control in it. I want to
I have a form which has a Combo Box Control. I have selected the
I have a vb6 form with an ocx control on it. The ocx control
I have a maximized form which has Combo-Box control (docked in top-right) with 500px

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.