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?
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.