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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:43:37+00:00 2026-05-24T22:43:37+00:00

I learnt the hard way when my multi-threaded app was modifying the same object

  • 0

I learnt the hard way when my multi-threaded app was modifying the same object and the List object couldn’t handle the two thread adding a object to the list at the exact same time.

I did some research and found i can use a lock to avoid this from happening, after a month of the application running smoothly it happend again even with the lock object in place. I don’t understand how this still cab be happening.
the error messages are the same as before:
1- Source array was not long enough. Check srcIndex and length, and the array’s lower bounds.
2- Index was outside the bounds of the array.

in the class private members I have this member variable

private static readonly object _syncLock = new object();

in the public function

public Dictionary<int, QueuedLetter> GetXMLFragment(Dictionary<int, QueuedLetter> queuedLetters, GroupNode GroupNode, GroupNode ParentGroupNode, StoredProcedure storedProcedure)

This is where I use the _synclock object the lines of code are as followed

XmlFragment xf = new XmlFragment();
xf.Fragment = xe;
xf.LetterQueueOID = lq.LetterQueueOID;
xf.ParentGroupNodeName = ParentGroupNodeName;
xf.LinkingField = GroupNode.LinkingField;
xf.GroupNodeName = GroupNode.GroupNodeName;

lock (_syncLock)
{
   if (lq.Fragments == null)
      lq.Fragments = new List<XmlFragment>();
   lq.Fragments.Add(xf);
}

I use a thread pool to create the multi-Threading functionality in another class it calls a starting function in the same class as where the lock happens above

events[i] = new AutoResetEvent(false);
AttachFragments f = new AttachFragments(events[i], groupNodeOID, QueuedLetters, string.Format("Failed while fragment Data : {0}, was executing ", groupNodeName));
f.OnException += ThreadErroredOut;
ThreadPool.QueueUserWorkItem(new WaitCallback(f.UpdateFragments));

If you can give me some advise on what I am doing wrong, that would be greatly appreciated.

I have thought about using
the Thread-Safe Collections in .NET 4.0 http://msdn.microsoft.com/en-us/library/dd997305.aspx
but I haven’t used it and if i change it to this, this would cause big changes in the code which would require a more in depth testing by are Q.A. team.

The stack traces of the two exceptions:
1- Source array was not long enough. Check srcIndex and length, and the array’s lower bounds.:
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable) at System.Collections.Generic.List1.Insert(Int32 index, T item) at JE.ThunderHead.Business.AttachFragments.GetXMLFragment(Dictionary2 queuedLetters, GroupNode GroupNode, GroupNode ParentGroupNode, StoredProcedure storedProcedure) in C:\TFS\LMS\Release\LM_05\LM_05.01\Solutions\ThunderHead\JE.Thunderhead.Business\AttachFragments.cs:line 135 at JE.ThunderHead.Business.AttachFragments.UpdateFragments(Object stateinfo) in C:\TFS\LMS\Release\LM_05\LM_05.01\Solutions\ThunderHead\JE.Thunderhead.Business\AttachFragments.cs:line 99

2- Index was outside the bounds of the array:
at System.Collections.Generic.List1.Add(T item) at JE.ThunderHead.Business.AttachFragments.GetXMLFragment(Dictionary2 queuedLetters, GroupNode GroupNode, GroupNode ParentGroupNode, StoredProcedure storedProcedure) in C:\TFS\LMS\Release\LM_05\LM_05.01\Solutions\ThunderHead\JE.Thunderhead.Business\AttachFragments.cs:line 231

  • 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-24T22:43:38+00:00Added an answer on May 24, 2026 at 10:43 pm

    From the stack trace you posted, it looks like there’s an Insert on that list that you’re not showing here. If that’s the case, you’ll need to lock around that Insert as well using the same lock object.

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

Sidebar

Related Questions

I learnt the hard way that it is not possible to slipstream Visual Studio
I've just learnt (the hard way) that databinding doesn't work with fields. This isn't
I have learnt the factory method design pattern, and at the same time, I
I'm doing Zed Shaw's fantastic Learn Python The Hard Way , but an extra
Working my way through Learn Python the Hard Way ex.25, and I just can't
I have been working on Learn Python the Hard Way 2nd Ed and it
I have this code from Learn Python The Hard Way and I need to
I'm learning Python using Learn Python The Hard Way . It is very good
I apologize if this has already been asked a different way but I couldn't
I am studying the Learn Python The Hard Way PDF. On page 82 i

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.