继续获取ArgumentOutOfRangeException

时间:2014-12-09 01:44:23

标签: c#

我开始变得疯狂。

我不断变得愚蠢ArgumentOutOfRangeException,我无法弄清楚如何解决它。我有以下代码:

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();
    }
}

以下方法是RequestLoanQuote()调用的网络服务的一部分:

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;

}

1 个答案:

答案 0 :(得分:1)

将代码更改为

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

或做类似的事情

    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);
        }
    }

问题是因为如果您使用quantity变量,那么您将超出列表的当前内容。

假设您有quantity=10,但您的列表只有list.Count=5,当您访问索引时,您将获得ArgumentOutOfRangeException