Question

I have a small code sample:

private void MonitorItems()
        {
            if (someCondition)
            {
                dateSelected = DateTime.Now;
                GetAllItems();
            }
            else
            {
                if(allItems.Count>0)
                    CheckAllItems();
            }
            MonitorItems();
        }

The Method GetAllItems goes to DB and get all new items for the collection -> allItems. Then, the CheckAllItems method:

private void CheckAllItems()
        {
            foreach (Item a in new List<Item>(allItems))
            {
                switch (a.Status)
                {
                    case 1:
                        HandleStatus1();
                        break;
                    case 2:
                        HandleStatus2(a);
                        break;
                    case 0:
                        HandleStatus0(a);
                        break;
                    default:
                        break;
                }
            }  
        }

In some cases (in HandleStatus1 and HandleStatus2) i need to go to the DB, make some updates, and then again populate the collection allItems with calling the method GetAllItems.

This type of code is throwing Stack.Overflow exception in WinFormsApp. I have two questions:
1. Is this type of exception will be thrown in WinService application, using the same code?
2. What is your opinion of using timers instead of self-calling method?

Was it helpful?

Solution

A "self-calling method" is more correctly called a "recursive method". Your solution is creative, I'll give you that. But don't do it. Stack space is very limited. You will see this problem when you move to a service, and there are much better ways of handling this. A timer is very appropriate when used in a service.

OTHER TIPS

Recursive calling the method in your case is as bad as using a timer to do it. You should do neither!!

Just use a simple loop and send the thread sleeping for some time in between.

MS IL has .tail op code. But c# dot want to recognize tail recursion (. By the way, tail recursion is so slow in .net ((

Why do you need to recurse at all? There is no flow control statement that will allow the method to stop recursing and exit the chain. The infinite recursions is probably what is causing the overflow. A better solution would to do away with the recursion altogether. Removing the else wrapper accomplishes the same result without having to recurse:

private void MonitorItems()
{
    if(someCondition)
    {
        dateSelected = DateTime.Now;
        GetAllItems();
    }
    if(allItems.Count>0)
        CheckAllItems();
}

This will accomplish the same result without getting stuck in a loop. Then you can implement rules to repeat the call in the context of the execution environment: a button click on a form or a timer on a service application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top