C#递归函数方法?

时间:2015-05-14 13:29:49

标签: c# recursion

我在Windows服务中有一个递归函数。完成后,此函数会自动倒带,因为它在递归中已重复多次。这不是开销吗?

有没有办法避免放松?有没有更好的方法?

编辑:在这个方法中,我从DB获取100条记录,然后处理它们,然后再获得100条,依此类推,直到DB中的所有记录都被处理完毕。

此外,数据库中可能存在的总记录数没有限制,因此该功能可以重复很多次。

public void ServiceFunctionality()
{
    try
    {
        // Get Data From WEBAPI
        HttpClient client = new HttpClient();
        HttpResponseMessage response = response = client.GetAsync("webapi url link").Result;
        Response<ServiceWrapper> objResponse = response.Content.ReadAsAsync<Response<ServiceWrapper>>().Result;

        if (objResponse != null)
        {
            if (objResponse.isSuccess == true)
            {
                listContact = objResponse.data.lContact;
                int MaxPKinSelectedRecords = objResponse.data.MaxPKinSelectedRecords;
                int MaxPKinTotalRecords = objResponse.data.MaxPKinTotalRecords;

                if (listContact != null && listContact.Count>0)
                {
                    try
                    {
                        Parallel.ForEach(listContact, contact =>
                        {
                            // some code...
                        });
                        // Recursive Call
                        if (MaxPKinTotalRecords != MaxPKinSelectedRecords)
                        {
                            ServiceFunctionality();
                        }
                    }
                    catch (Exception ex)
                    {
                        // Logging
                    }
                }
            }
            else
            {
                // Logging
            }
        }
        else
        {
            // Logging
        }
    }
    catch (Exception ex)
    {
        // Logging
    }
} 

2 个答案:

答案 0 :(得分:3)

您可以随时放松到while循环。因为你的调用没有改变状态,所以这很简单。

public void ServiceFunctionality()
{
    bool done = false;
    while(!done) {
    try
    {
        done = true; //if we don't reset this, we're done.
        // Get Data From WEBAPI
        HttpClient client = new HttpClient();
        HttpResponseMessage response = response = client.GetAsync("webapi url link").Result;
        Response<ServiceWrapper> objResponse = response.Content.ReadAsAsync<Response<ServiceWrapper>>().Result;

        if (objResponse != null)
        {
            if (objResponse.isSuccess == true)
            {
                listContact = objResponse.data.lContact;
                int MaxPKinSelectedRecords = objResponse.data.MaxPKinSelectedRecords;
                int MaxPKinTotalRecords = objResponse.data.MaxPKinTotalRecords;

                if (listContact != null && listContact.Count>0)
                {
                    try
                    {
                        Parallel.ForEach(listContact, contact =>
                        {
                            // some code...
                        });
                        // set loop variable
                        if (MaxPKinTotalRecords != MaxPKinSelectedRecords)
                        {
                            done = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        // Logging
                    }
                }
            }
            else
            {
                // Logging
            }
        }
        else
        {
            // Logging
        }
    }
    catch (Exception ex)
    {
        // Logging
    }
} 
}

答案 1 :(得分:1)

只要有其他合适的解决方案,就不要使用递归来调用函数。我个人几乎从未做过

除了使用一段时间之外,我试图让它保持不变..

不要忘记打破你的循环。我试图处理这件事,但仍然

要非常小心,不要冒服务器无限循环的风险我拿了maxPossibleIterations。因此,如果出现任何错误,您的Web服务服务器就不必进行无限次迭代

public void ServiceFunctionality()
{
    long maxPossibleIterations = 999999;
    try
    {
        while (true)
        {
        maxPossibleIterations++;
        // Get Data From WEBAPI
        HttpClient client = new HttpClient();
        HttpResponseMessage response = response = client.GetAsync("webapi url link").Result;
        Response<ServiceWrapper> objResponse = response.Content.ReadAsAsync<Response<ServiceWrapper>>().Result;

        if (objResponse != null)
        {
            if (objResponse.isSuccess == true)
            {
                listContact = objResponse.data.lContact;
                int MaxPKinSelectedRecords = objResponse.data.MaxPKinSelectedRecords;
                int MaxPKinTotalRecords = objResponse.data.MaxPKinTotalRecords;

                if (listContact != null && listContact.Count>0)
                {
                    try
                    {
                        Parallel.ForEach(listContact, contact =>
                        {
                            // some code...
                        });
                        if (MaxPKinTotalRecords == MaxPKinSelectedRecords)
                        {
                            break;
                        }                        
                    }
                    catch (Exception ex)
                    {
                        // Logging
                    }
                }
                else
                    break; //Important
            }
            else
            {
                // Logging
                break;
            }
        }
        else
        {
            // Logging
            break;
        }
        } // End while
    }
    catch (Exception ex)
    {
        // Logging
    }
}