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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:02:03+00:00 2026-06-16T06:02:03+00:00

I’m working on a Windows Service that cycles through a series of databases and

  • 0

I’m working on a Windows Service that cycles through a series of databases and then sends SMS messages using HTTP requests. In the beginning of the method that goes through each database, I define a generic list as follows:

public static List<Recipient> Recipients = new List<Recipient>();

I populate that list with the results of an asynchronous HTTP request sent through a thread pool:

//inside main method                    
foreach (var SMS in SMSJoin)
{
    ...

    SMSMessage oSMS = new SMSMessage(Keyword, Number, Message, MessageID);

    ThreadPool.QueueUserWorkItem(SendSMS, oSMS);
}

Then it gets passed to the next method:

public static void SendSMS(object SMStoSend)
{
    try
    {
        SMSMessage oSMS = (SMSMessage)SMStoSend;
        ...
        Request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), state);
    }
    ...
}

Then to GetRequestStreamCallback…

public static void GetRequestStreamCallback(IAsyncResult AsynchronousResult)
{
    State state = (State)AsynchronousResult.AsyncState;
    SMSMessage oSMS = state.oSMS;
    try
    {
        ...
        Request.BeginGetResponse(new AsyncCallback(ReceiveResponse), state);
    }
    ...
}

And finally the response is received and added to the Recipients List:

public static void ReceiveResponse(IAsyncResult Result)
{
    ...
    finally
    {
        oSMS.Complete = DateTime.Now;

        Recipient CompleteMessage = new Recipient(oSMS.MessageID, oSMS.Recipient, oSMS.ErrorCode, oSMS.Complete, oSMS.ResponseCode);
        Recipients.Add(CompleteMessage);
    }

At the end of the code, each response should be added to a Recipient object and then stored in the generic list. The problem is that once about every 1000 or so SMS messages (sent in batches of 50), there is an unhandled IndexOutOfRangeException thrown by the line of code Recipients.Add(CompleteMessage).

I didn’t think that there were indexes in the list, and many of the recipients are duplicates. Why would it throw this error, and why only every once in a good while? Could this have to do with threading issues?

As a note, I’m activating the main method from a Timer object, but it will wait until the previous instance is complete before starting a new instance.

Edit:
Here is the Recipient class:

public class Recipient
{
    public Recipient(long MessageID, string PhoneNumber, string ErrorMessage, DateTime? Completed, string Response)
    {
        this.MessageID = MessageID;
        this.PhoneNumber = PhoneNumber;
        this.ErrorMessage = ErrorMessage;
        this.Completed = Completed;
        this.Response = Response;
    }

    public long MessageID { get; set; }
    public string PhoneNumber { get; set; }
    public string ErrorMessage { get; set; }
    public DateTime? Completed { get; set; }
    public string Response { get; set; }
}
  • 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-16T06:02:06+00:00Added an answer on June 16, 2026 at 6:02 am

    My guess would that you’re running into multiple threads trying to add to Recipients at the same time and one beginning before the prior has finished.

    Try locking your Recipients during read and writes and see if it helps:

    private static object mylock = new object();
    private static List<Recipient> _recipients = new List<Recipient>();
    public static List<Recipient> Recipients
    {
        get
        {
            List<Recipient> result;
            lock (mylock)
            {
                result = _recipients;
            }
            return result;
        }
        set
        {
            lock (mylock)
            {
                _recipients = value;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am using JSon response to parse title,date content and thumbnail images and place
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

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.