楼上的指的是无限调用这个AsynCallBack函数,还是在这个AsynCallBack函数中出现了无限递归的死循环呢?

解决方案 »

  1.   

    StackOverflowException一般都很难查。
    因为没有提供相关堆栈的跟踪。
    但其中肯定是有递归的。
    你先检查一下哪些方法有递归调用。
      

  2.   

    两个异步调用的函数代码如下:
    private delegate void dlgSendToDaemon(); private delegate void dlgSendStatusOp(); private void cbSendStatusOp(IAsyncResult ar)
    {
    dlgSendStatusOp dlgt = (dlgSendStatusOp)ar.AsyncState;
    dlgt.EndInvoke(ar);
    } private void cbRecvFromDaemon(IAsyncResult ar)
    {
    dlgRecvFromDaemon dlgt = (dlgRecvFromDaemon)ar.AsyncState;
    dlgt.EndInvoke(ar);
    bTaskDoing = false;
    }private void SendToDaemon()
    {
    KeyValues kv1 = new KeyValues(10);
    kv1.Add("op", "test");
    SendList.Add(kv1.ToString());
    DateTime dtPreTest = DateTime.Now;
    int iTestLinkTime = 60;
    if(ConfigurationSettings.AppSettings["TestLinkTime"] != null)
    iTestLinkTime = int.Parse(ConfigurationSettings.AppSettings["TestLinkTime"]);
    for(;;)
    { try
    {
    int iTimeSpan = ((TimeSpan)(DateTime.Now - dtPreTest)).Seconds;
    if( iTimeSpan >= iTestLinkTime)
    {
    string strTestLink = "";
    KeyValues kv = new KeyValues(10);
    kv.Add("op", "test");
    strTestLink = kv.ToString();
    SendList.Add(strTestLink);
    dtPreTest = DateTime.Now;
    }
    if(!socket.Connected)
    {
    IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ConfigurationSettings.AppSettings["RemoteIP"]), 
    int.Parse(ConfigurationSettings.AppSettings["RemotePort"]));
    socket.Connect(ipe);
    } if(SendList.Count <= 0)
    {
    System.Threading.Thread.Sleep(1000);
    continue;
    }
    string strSend = SendList[0].ToString();
    socket.Send(Encoding.ASCII.GetBytes(strSend), 0, strSend.Length, SocketFlags.None);
    if(SendList.Count <= 0)
    {
    System.Threading.Thread.Sleep(1000);
    continue;
    }
    SendList.RemoveAt(0);
    if(SendList.Count <= 0)
    {
    System.Threading.Thread.Sleep(500);
    }
    else
    continue;
    }
    catch(SocketException x)
    {
    lbError.Text = "SocketException : "  + strError + x.Message;
    System.Threading.Thread.Sleep(1000);
    continue;
    }
    catch(ObjectDisposedException)
    {
    (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    continue;
    }
    catch(Exception x)
    {
    lbError.Text = "SendError : "  + strError + x.Message;
    if( SendList == null)
    {
    SendList = new ArrayList();
    continue;
    }
    System.Threading.Thread.Sleep(1000);
    continue;
    }
    }
    } private void RecvFromDaemon()
    {
    byte[] rcvBuff = new byte[8196];
    while(true)
    {
    int nCount = 0;
    try
    {
    if(!socket.Connected)
    {
    IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ConfigurationSettings.AppSettings["RemoteIP"]), 
    int.Parse(ConfigurationSettings.AppSettings["RemotePort"]));
    socket.Connect(ipe);
    }

    strRecvError = "--1--";
    int iRecvCount = 0;
    if(socket.Available > 8196)
    iRecvCount = 8196;
    else
    iRecvCount = socket.Available;
    nCount = socket.Receive(rcvBuff, nCount, iRecvCount, SocketFlags.None);
    //如果未接收到信息,则休眠1秒后继续等待接收信息
    if(nCount <= 0)
    { System.Threading.Thread.Sleep(1000);
    continue;
    }
    //接收到信息则处理信息
    if(strRecv != "")
    {
    strRecv += Encoding.Default.GetString(rcvBuff);
    }
    else
    {
    strRecv = Encoding.Default.GetString(rcvBuff);
    }
    Array.Clear(rcvBuff, 0, rcvBuff.Length);
    while(strRecv != "")
    {
    if(strRecv.Length <= 0)
    {
    strRecv = "";
    continue;
    }
    else if(strRecv[0] == '\0')
    {
    strRecv = "";
    continue;
    } AnalysisRecv( ref strRecv);
    continue;
    }
    System.Threading.Thread.Sleep(int.Parse(ConfigurationSettings.AppSettings["RefreshInternal"]) * 100);

    }
    catch(SocketException x)
    { if(x.ErrorCode == 10053)//您的主机中的软件放弃了一个已建立的连接。
    {
    socket.Close();
    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    }
    System.Threading.Thread.Sleep(1000);
    continue;
    }
    catch(ObjectDisposedException)
    {
    socket.Close();
    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    continue;
    }
    catch(Exception x)
    {
    lbError.Text = "RecvError : "+ strRecvError + x.Message;
    System.Threading.Thread.Sleep(1000);
    continue;
    }
    }
    }
      

  3.   

    那里用到了cbRecvFromDaemon和cbSendStatusOp?楼主怎么没贴出来
      

  4.   

    sorry
    忘记贴出来了
    调用在page_load中
    如下:(iRecvThrCount 和iSendThrCount 用来控制调用次数,当调用了之后就++,否则,调用结束的时候--)
    try
    {
    dlgRecvFromDaemon dlgtRecv = new dlgRecvFromDaemon(RecvFromDaemon);
    if(iRecvThrCount < 1)
    {
    dlgtRecv.BeginInvoke(new AsyncCallback(cbRecvFromDaemon), dlgtRecv);
    }

    dlgSendToDaemon dlgtSend = new dlgSendToDaemon(SendToDaemon);
    if(iSendThrCount < 1)
    {
    dlgtSend.BeginInvoke(new AsyncCallback(cbSendToDaemon), dlgtSend);
    }
    }
    catch(Exception x)
    {
    lbError.Text = "NewAsyncCallError : " + x.Message;
    return;
    }
      

  5.   

    把出错的异常的堆栈打印出来:
    lbError.Text = x.StackTrace;