Reputation: 5319
I have endless process that work with the next logic (notice the circle between ServiceUtilities_OnReSubscribing
to timer_Elapsed
) - when exception is raised in ServiceUtilities_OnReSubscribing, it's create a new timer t
and in the finish" event of 't', a call to ServiceUtilities_OnReSubscribing is scheduled.
I'm wondering how C# stack is behaving in this case, is it considered a recursion?
System.Timers.Timer timer;
readonly object timerLocker = new object();
void ServiceUtilities_OnReSubscribing()
{
lock (timerLocker)
{
try
{
//do something
}
catch (Exception ex)
{
//do somehing...
timer = SetTimer();
timer.Start();
}
}
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
CleanTimer(timer);
ServiceUtilities_OnReSubscribing();
}
private System.Timers.Timer SetTimer()
{
int miliSeconds = 0;
if (int.TryParse(ConfigurationManager.AppSettings["ReSubscribingTimer"], out miliSeconds) == false)
miliSeconds = 3000;
if (miliSeconds <= 1000)
miliSeconds = 3000;
System.Timers.Timer timer = new System.Timers.Timer(miliSeconds);
timer.Enabled = true;
timer.Elapsed += timer_Elapsed;
return timer;
}
private void CleanTimer(System.Timers.Timer timer)
{
timer.Elapsed -= timer_Elapsed;
timer.Dispose();
}
I want to avoid a scenario which my stacktrace gets full due a many recursive calls. Is this code considered as recursion?
Upvotes: 2
Views: 112
Reputation: 20764
Your timer_Elapsed
method is called from the timer thread, so it is not on the same call stack as ServiceUtilities_OnReSubscribing
.
Therefore you don't need to worry about your stack overflowing, as the call stack won't grow.
Upvotes: 1