如何在c#中停止两个函数之间的循环 [英] How do I Stop the Looping Between Two Functions in c#

查看:87
本文介绍了如何在c#中停止两个函数之间的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个函数我在其中调用函数一,它返回实际输出,但当两个函数一次又一次返回相同值时,我想停止循环,但它不会停止,它仍然是一个无限循环继续,所以请告诉我如何停止循环?

我的代码是......

Hi,I have Two Functions In which I am calling function one inside one ,It returns Actual output,But when Both the function returns Same value again and again then I want to Stop The Looping,But it doesn't Stop,It still Continue as an endless loop,So Please tell me how can I stop the looping?
My code is...

protected void RelativeFamilyIDFrom(ArrayList RelativeMemberID)
    {
        ArrayList listFamilyID = new ArrayList();

        var strings = RelativeMemberID.Cast<string>().ToArray();

        String s = String.Join(",", strings);
        
            //Find all FamilyID related to member
            sSQL = "SELECT DISTINCT AF_FamilyID FROM AllFamily_Master";
            sSQL = sSQL + " WHERE AF_MemberID IN(" + s + ")";
            

            DS = new DataSet();
            da = new OdbcDataAdapter(sSQL, conn);
            da.Fill(DS);
            
            if (DS.Tables.Count > 0)
            {
                if (DS.Tables["table"].Rows.Count > 0)
                {
                    for (int i = 0; i < DS.Tables["table"].Rows.Count; i++)
                    {
                        listFamilyID.Add(Convert.ToDouble(DS.Tables["table"].Rows[i]["AF_FamilyID"].ToString()));
                    }
                }
            }
         RelativeMemberIDOfFamily(listFamilyID);

       // CompareArrayList(listFamilyID, Convert.ToDouble(lblUserToFamilyID.Text.ToString()));
    }

    protected void RelativeMemberIDOfFamily(ArrayList RelativeFamilyID)
    {
        ArrayList listFamilyMemberID = new ArrayList();

        String t = String.Join(",", RelativeFamilyID.ToArray());
       
            sSQL = "SELECT DISTINCT AF_MemberID FROM AllFamily_Master";
            sSQL = sSQL + " WHERE AF_FamilyID IN(" + t + ")";

            DS1 = new DataSet();
            da1 = new OdbcDataAdapter(sSQL, conn);
            da1.Fill(DS1);

            if (DS1.Tables.Count > 0)
            {
                if (DS1.Tables["table"].Rows.Count > 0)
                {
                    for(int k=0;k<DS1.Tables["table"].Rows.Count;k++)
                    {
                        listFamilyMemberID.Add(DS1.Tables["table"].Rows[k]["AF_MemberID"].ToString());
                    }
                  
                }
            }
        
        RelativeFamilyIDFrom(listFamilyMemberID);
    }

推荐答案

我不太确定你要做什么,但没有办法解决这个问题码。让我通过删除视觉浪费来简化它:

I'm not quite sure what you are trying to do, but there is no way out with that code. Let me simplify it a bit by removing the "visual waste":
protected void RelativeFamilyIDFrom(ArrayList RelativeMemberID)
    {
         ... Do something
         RelativeMemberIDOfFamily(listFamilyID);
    }
 
    protected void RelativeMemberIDOfFamily(ArrayList RelativeFamilyID)
    {
        ... Do something
        RelativeFamilyIDFrom(listFamilyMemberID);
    }

只要你调用任何一种方法,它就会一直调用另一种方法,并且会一直这样做,直到堆栈用完并且你的应用程序崩溃。

在任何一种例程中都没有代码可以防止在任何情况下使用。



现在,我不能说他们在做什么,或者他们应该或不应该做什么互相打电话,但你需要添加某种形式的测试,以防止在某些时候 - 或无限递归将继续。



就个人而言,我认为你实际上需要坐下来思考你在那里所拥有的东西,因为这些方法在现实世界中所做的总和非常缓慢 - 他们不会对他们检索的数据做任何事情,除非将它传递给对方!它永远不会离开方法所以你可以用这个替换你的两个方法来解决问题:

As soon as you call either method, it will always call the other, and will continue to do so until the stack runs out and your application crashes.
There is no code in either routine to prevent that under any circumstances.

Now, I can't say what they are doing, or when they should or shouldn't call each other, but you need to add a test of some form to prevent that at some point - or the infinite recursion will continue.

Personally, I think you actually need to sit down and think about what you have there, because the total sum of what these methods do in the real world is very slowly crash - they do not do anything with the data they retrieve except pass it to each other! It never leaves the method so you could best fix the problem by replacing your two methods with this:

protected void RelativeFamilyIDFrom(ArrayList RelativeMemberID)
    {
    }
 
    protected void RelativeMemberIDOfFamily(ArrayList RelativeFamilyID)
    {
    }

你的应用程序不会崩溃,也不会有任何真正的改变你的应用程序有效......

And your app wouldn't crash, nor would there be any real change in how your application works...


你需要添加一个深度变量:

You need to add a depth variable :
protected void RelativeFamilyIDFrom(ArrayList RelativeMemberID, ref int depth)
    {
          if(++depth >10) return; // stop at 10 levels deep for example
...
          RelativeMemberIDOfFamily(listFamilyID, ref depth);
}

protected void RelativeMemberIDOfFamily(ArrayList RelativeFamilyID)
    {
          if(++depth >10) return; // stop at 10 levels deep for example
...
          RelativeFamilyIDFrom(listFamilyMemberID, ref depth);
}


void main()
{
   int depth = 0; // start at 0
   RelativeFamilyIDFrom(listFamilyMemberID, ref depth); // call your first method
}


这篇关于如何在c#中停止两个函数之间的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆