ContextSwitchDeadlock error, index out of range and < size collect

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SkI=?=

    ContextSwitchDeadlock error, index out of range and < size collect

    Hello

    As I debug the C# code with a break point and by pressing F11 I
    eventually get a message stating:

    ContextSwitchDe adlock was detected
    Message: The CLR has been unable to transition from COM context 0x17aeb8 to
    COM context 0x17abd8 for 60 seconds. The thread that owns the destination
    context/apartment is most likely either doing a non pumping wait or
    processing a very long running operation without pumping Windows messages.
    This situation generally has a negative performance impact and may even lead
    to the application becoming non responsive or memory usage accumulating
    continually over time. To avoid this problem, all single threaded apartment
    (STA) threads should use pumping wait primitives (such as
    CoWaitForMultip leHandles) and routinely pump messages during long running
    operations.

    then eventually I get the index out of range message.

    If I take out the break point and I dont press F11 to debug and just select
    Debug - Start Debugging then I just get the message:

    Index was out of range. Must be non-negative and less than size of the
    collection. Parameter name:index.

    What do you handle these types of errors? The error does not happen in the
    first half of the program where the list is created it happens in the second
    half on the second on line 236 (however it is actually one dll call by a
    form). I have pointed the first half and the second half of the program and
    I have pointed out where line 236 is in the second half in the code so you
    know what line in the code it is bombing on:


    //****this is the first half of the program which has no problem
    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Collecti ons;
    using System.Data;
    using System.Data.Sql Client;
    using System.Windows. Forms;
    using System.Text.Reg ularExpressions ;
    using System.Threadin g;

    namespace NotUsedProj
    {
    class NotUsedList
    {
    private int ADDist;
    private int EDDist;

    public NotUsedList(int ADD, int EDD)
    {
    ADD = ADDist;
    EDD = EDDist;
    }

    public int ADD
    {
    get { return ADDist; }
    set { ADDist = value; }
    }

    public int EDD
    {
    get { return EDDist; }
    set { EDDist = value; }
    }

    public static List<objectNotU sedDLList()
    {
    List<objectdtLi st = new List<object>();
    List<objectNotU sedTbl = new List<object>();
    List<objectRetN otUsed = new List<object>();
    dtList = NotUsedData.Get DLData();

    int AEsubtotal = 0;
    string AEspace = " ";
    int HoldAD = Convert.ToInt32 (dtList[0]);
    int HoldED = Convert.ToInt32 (dtList[1]);
    int CurrAD = 0;
    int CurrED = 0;
    //int NotUsed = dtList.Count /2;
    int FromED = 0;
    string ToEd = " ";
    string nuRange = " ";
    string isRange = " ";
    int grandTotalED = 0;
    int noFirst = 0;

    //NotUsedTbl.Clea r();
    //MessageBox.Show ("total recs in dtlist is " + dtList.Count);
    for (int i = 2; i <dtList.Count ; i++)
    {
    CurrAD = Convert.ToInt32 (dtList[i]);
    i++;
    CurrED = Convert.ToInt32 (dtList[i]);
    if ((HoldAD == CurrAD) & (CurrED == (HoldED + 1)))
    {
    AEsubtotal++;
    if ((noFirst == 0) & (isRange == "N") & (ToEd == " "))
    {
    AEsubtotal = AEsubtotal + 1;
    grandTotalED = grandTotalED + 2;
    noFirst = 1;
    }
    else
    grandTotalED++;
    NotUsedTbl.Add( HoldAD.ToString ());

    if (isRange == "N" | isRange == " ")
    {
    FromED = HoldED;
    ToEd = CurrED.ToString ();
    }

    if (isRange == "Y")
    {
    ToEd = CurrED.ToString ();
    }

    nuRange = FromED.ToString () + " To " + ToEd;
    NotUsedTbl.Add( nuRange );
    NotUsedTbl.Add( AEsubtotal);
    NotUsedTbl.Add( grandTotalED);
    isRange = "Y";
    HoldAD = CurrAD;
    HoldED = CurrED;
    }

    if ((HoldAD == CurrAD) & (CurrED (HoldED + 1)))
    {
    AEsubtotal++;
    grandTotalED++;
    if (isRange == "N")
    {
    NotUsedTbl.Add( CurrAD.ToString ());
    FromED = CurrED;
    nuRange = FromED.ToString ();
    NotUsedTbl.Add( nuRange);
    }
    else
    {
    NotUsedTbl.Add( HoldAD.ToString ());
    FromED = HoldED;
    nuRange = FromED.ToString ();
    NotUsedTbl.Add( nuRange);
    }
    NotUsedTbl.Add( AEsubtotal);
    NotUsedTbl.Add( grandTotalED);
    isRange = "N";
    HoldAD = CurrAD;
    HoldED = CurrED;
    }
    if ((HoldAD < CurrAD))
    {
    AEsubtotal = 0;
    AEsubtotal++;
    grandTotalED++;
    if (isRange == "Y" | isRange == "N")
    {
    NotUsedTbl.Add( CurrAD.ToString ());
    FromED = CurrED;
    nuRange = FromED.ToString ();
    NotUsedTbl.Add( nuRange);
    }
    else
    {
    NotUsedTbl.Add( HoldAD.ToString ());
    FromED = HoldED;
    nuRange = FromED.ToString ();
    NotUsedTbl.Add( nuRange);
    }

    NotUsedTbl.Add( AEsubtotal);

    NotUsedTbl.Add( grandTotalED);

    HoldAD = CurrAD;
    HoldED = CurrED;
    isRange = "N";
    }
    ToEd = " ";
    }
    //MessageBox.Show (" total recs = " + NotUsedTbl.Coun t);
    //return NotUsedTbl;

    //*** this is the second half of the program and wher the problem is
    string contED;
    string retED;
    int ADhold = 0;
    string EDhold = " ";
    int ADcurr = 0;
    string EDcurr = " ";
    int svAD = 0;
    string svEd = " ";
    int ctSwitch = 0;
    int tempSub = 0;
    int tempGrand = 0;
    int svSub = 0;
    int svGrand = 0;
    int subHold = 0;
    int grandHold = 0;
    int z = 0;

    ADhold = Convert.ToInt32 (NotUsedTbl[z]); // this is the ad
    z++;
    EDhold = Convert.ToStrin g(NotUsedTbl[z]);// this is the ed
    retED = Convert.ToStrin g(EDhold);
    z++;
    svSub = Convert.ToInt32 (NotUsedTbl[z]); // this is the subtotal
    z++;
    svGrand = Convert.ToInt32 (NotUsedTbl[z]); //this is the grandtotal
    z++;
    ADcurr = Convert.ToInt32 (NotUsedTbl[z]); // another ad
    z++;
    EDcurr = Convert.ToStrin g(NotUsedTbl[z]); // another ed
    contED = Convert.ToStrin g(EDcurr);
    z++;
    tempSub = Convert.ToInt32 (NotUsedTbl[z]);
    z++;
    tempGrand = Convert.ToInt32 (NotUsedTbl[z]);

    for (int i = 7; i < NotUsedTbl.Coun t; i++)
    {
    if (ADhold == ADcurr)
    {
    while (ADhold == ADcurr)
    {
    if (retED.Length 2)
    retED = retED.Substring (0,3);
    bool findED = contED.StartsWi th(retED);
    if (findED)
    {
    ADhold = ADcurr;
    EDhold = EDcurr;
    z++;
    ADcurr = Convert.ToInt32 (NotUsedTbl[z]);
    retED = EDhold.ToString ();
    z++;
    EDcurr = Convert.ToStrin g(NotUsedTbl[z]);
    contED = EDcurr.ToString ();
    z++;
    svSub = Convert.ToInt32 (NotUsedTbl[z]);
    z++;
    svGrand = Convert.ToInt32 (NotUsedTbl[z]);
    //MessageBox.Show ("found at " + retED.Index);
    }
    else
    {
    RetNotUsed.Add( ADhold);
    RetNotUsed.Add( EDhold);

    if (ctSwitch == 0)
    {
    subHold = tempSub;
    grandHold = tempGrand;
    ctSwitch = 1;
    }
    else
    {
    subHold = svSub;
    grandHold = svGrand;
    ctSwitch = 1;
    }
    RetNotUsed.Add( subHold);
    RetNotUsed.Add( grandHold);
    ADhold = ADcurr;
    EDhold = EDcurr;
    retED = EDhold.ToString ();
    z++;
    //***
    //*** line 236 ADcurr =
    Convert.ToInt32 (NotUsedTbl[z]);
    //***
    z++;
    EDcurr = Convert.ToStrin g(NotUsedTbl[z]);
    contED = EDcurr.ToString ();
    z++;
    svSub = Convert.ToInt32 (NotUsedTbl[z]);
    z++;
    svGrand = Convert.ToInt32 (NotUsedTbl[z]);
    }
    }
    }
    else if (ADhold < ADcurr)
    {
    RetNotUsed.Add( ADhold);
    RetNotUsed.Add( EDhold);
    if (ctSwitch == 0)
    {
    subHold = tempSub;
    grandHold = tempGrand;
    ctSwitch = 1;
    }
    else
    {
    subHold = svSub;
    grandHold = svGrand;
    ctSwitch = 1;
    }
    RetNotUsed.Add( subHold);
    RetNotUsed.Add( grandHold);
    ADhold = ADcurr;
    EDhold = EDcurr;
    retED = EDhold.ToString ();
    z++;
    ADcurr = Convert.ToInt32 (NotUsedTbl[z]);
    z++;
    EDcurr = Convert.ToStrin g(NotUsedTbl[z]);
    contED = EDcurr.ToString ();
    z++;
    svSub = Convert.ToInt32 (NotUsedTbl[i]);
    z++;
    svGrand = Convert.ToInt32 (NotUsedTbl[z]);
    }
    }
    MessageBox.Show (" total recs = " + NotUsedTbl.Coun t);
    MessageBox.Show (" total recs = " + RetNotUsed.Coun t);
    return RetNotUsed;

    }
    }
    }






    JB
  • Peter Duniho

    #2
    Re: ContextSwitchDe adlock error, index out of range and &lt; sizecollect

    On Thu, 13 Nov 2008 10:59:01 -0800, JB <JB@discussions .microsoft.com>
    wrote:
    Hello
    >
    As I debug the C# code with a break point and by pressing F11 I
    eventually get a message stating:
    >
    ContextSwitchDe adlock was detected
    Message: The CLR has been unable to transition from COM context 0x17aeb8
    to
    COM context 0x17abd8 for 60 seconds. [...]
    >
    then eventually I get the index out of range message.
    >
    If I take out the break point and I dont press F11 to debug and just
    select
    Debug - Start Debugging then I just get the message:
    >
    Index was out of range. Must be non-negative and less than size of
    the
    collection. Parameter name:index.
    >
    What do you handle these types of errors?
    You can safely ignore the first error, assuming it only happens when you
    are stepping through code in the debugger. The act of doing so
    necessarily causes other threads (which are also suspended when you're
    just sitting there interrupted in the debugger) to not be able to work.
    Sometimes that causes the delay that sets off that warning.

    As for the second error, you need to look at your code when the exception
    happens and see why you are passing an index that's out of range.
    Obviously, it would be invalid to pass an index to a collection that is
    nothing within the range of valid indices for the elements in that
    collection.

    The code you posted is neither concise nor complete, so I'm not going to
    spend any time trying to figure out why your "z" index winds up larger
    than the maximum valid index for the "NotUsedTbl " list instance. But
    suffice to say, it is and that's exactly what that exception is telling
    you.

    Pete

    Comment

    Working...