OmniOwl
OmniOwl

Reputation: 5709

Keep getting ArgumentOutOfRangeException

I am starting to become insane.

I keep getting a silly ArgumentOutOfRangeException and I can't figure out how to fix it. I have the following code:

private void Send(List<SimpleRequest.LoanRequest> list)
{
    int delay = int.Parse(numericDelay.Value.ToString());

    for (int threadnumber = 0; threadnumber < list.Count; threadnumber++)
    {
        Task.Factory.StartNew(() => RequestLoanQuote(threadnumber, list[threadnumber]));
        Thread.Sleep(delay);
    }
}

private void RequestLoanQuote(object state, SimpleRequest.LoanRequest loanRequest)
{
    try
    {
        if (console.InvokeRequired)
        {
            AppendText("Sending: " + loanRequest.SSN + "\n");
        }

        StringBuilder sb = new StringBuilder();
        var threadnumber = (int)state;
        using (var client = new LoanBrokerWS.LoanBrokerWSClient())
        {
            Utility_Tool.LoanBrokerWS.LoanQuote response = client.GetLoanQuote(loanRequest.SSN, loanRequest.LoanAmount, loanRequest.LoanDuration);
            sb.Append(response.SSNk__BackingField + " returned: ");
            sb.Append(response.interestRatek__BackingField + " | ");
            sb.Append(response.BankNamek__BackingField + "\n");
            AppendText(sb.ToString());
        }
    }
    catch (Exception e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("--- ");
        sb.Append(loanRequest.SSN);
        sb.Append(" ---\n" + e.Message + "\n--- ");
        sb.Append(loanRequest.SSN);
        sb.Append(" END ---\n");
        AppendText(sb.ToString());
    }
}

private void AppendText(String msg)
{
    Action append = () => console.AppendText(msg);
    if (this.console.InvokeRequired)
    {
        console.BeginInvoke(append);
    }
    else
    {
        append();
    }
}

The below method is part of the webservice that RequestLoanQuote() calls:

public LoanQuote GetLoanQuote(string ssn, double amount, DateTime loanDuration)
{

    LoanRequest loanrequest = new LoanRequest
    {
        SSN = ssn,
        LoanAmount = amount,
        LoanDuration = loanDuration
    };

   ssn= ssn.Replace("-", "");

    Message loanMessage = new Message();
    loanMessage.Body = loanrequest;


    requestQueue.Send(loanMessage);
    Console.WriteLine("sent message for " +ssn);
    CollectionKeeper.sentmessages++;
    //finding the right response 
    LoanQuote rightquote = null;

    while (rightquote == null)
    {
        // Console.WriteLine( ssn+" is waiting for response");
        Thread.Sleep(200);

        var t = CollectionKeeper.quotes.TryRemove(ssn, out rightquote);



    }
    Console.WriteLine(ssn + " found response");
    Console.WriteLine(CollectionKeeper.sentmessages);
    return rightquote;

}

Upvotes: 0

Views: 197

Answers (1)

DevEstacion
DevEstacion

Reputation: 1977

Change your code either to

    for (int threadnumber = 0; threadnumber < list.Count; threadnumber++)
    {
        Task.Factory.StartNew(() => RequestLoanQuote(threadnumber, list[threadnumber]));
        if (delay > 0)
        {
            Thread.Sleep(delay);
        }
    }

or do something like this

    int actualLimit = quantity > list.Count ? list.Count : quantity;
    for (int threadnumber = 0; threadnumber < actualLimit; threadnumber++)
    {
        Task.Factory.StartNew(() => RequestLoanQuote(threadnumber, list[threadnumber]));
        if (delay > 0)
        {
            Thread.Sleep(delay);
        }
    }

The problem is because you're going outside the current content of the list if you use the quantity variable.

Lets say you had quantity=10 but your list only has list.Count=5, you'd be getting the ArgumentOutOfRangeException when you access the index.

Upvotes: 1

Related Questions