Building Timeout in C#

  • Thread starter Thread starter Pietje de kort
  • Start date Start date
P

Pietje de kort

Hello all,

I am trying to build a class that does something, and may timeout while
doing so. Ofcouse I want to be a bit elegant, so I came up with the code
found below. Problem is, I can't catch the exception thrown on timeout.
Anybody knows how to correctly implement timeout behaviour?

Best regards, Wouter van Vugt
 
haha, oops that was a bit dumb.
Here it is, it starts a timer class (System.Threading)
that notifies by calling a delegate. The delegate method
that is called throws an exception indicating timeout. Problem
is that I can't catch the exception.


public class test
{
public test(){}

public void DoTimeout()
{

Timer t = new Timer(new TimerCallback(Timedout),
this,1000,Timeout.Infinite);
try
{
while(true)
{
Console.WriteLine("Doing something");
Thread.Sleep(100000);
}
}
catch(Exception e)
{
// catch the timeout exception
Console.WriteLine(e.Message);
}

}

private void Timedout(object state)
{
throw new ApplicationException("Timedout");
}
}
 
You throw exceptions for NORMAL status? You are a very inefficent
developer. Exceptions are for "exceptional" circumstances, not for status or
return codes.
 
You throw exceptions for NORMAL status? You are a very inefficent
developer. Exceptions are for "exceptional" circumstances, not for
status or return codes.

Why NORMAL status?
He wants to throw a exception if his code runs into a timeout.
And he wants to catch this timeout-exception.

Till Meyer
 
Exactly Till,
I always thought that a timeout was abnormal, and
who cares. I want a good Timeout mechanism, preferably
the one I posted here earlier, currently I don't throw an
exception in the Timedout() method, but set a bool 'timedout'
to true which I check in the while() loop. I don't think it's
particularly elegant, but it works.

Anybody with any ideas, without flaming some EXAMPLE code.

Laters! Wouter
 
Not sure I understand the problem but:

public class PModel
{

private System.Timers.Timer watchDogTimer;


public PModel()
{

watchDogTimer = new System.Timers.Timer(1000); //1 second timeout
watchDogTimer.Enabled = false;
watchDogTimer.AutoReset = true;

watchDogTimer.Elapsed+=new ElapsedEventHandler(OnWatchDogBark);

}


public void OnWatchDogBark(object source, ElapsedEventArgs e)
{


watchDogTimer.Enabled = false;

/*do something here on a time out*/

watchDogTimer.Enabled = true;


}
}


You could enable the timer in a routine other than the constructor if that
makes more sense.


Pietje de kort said:
Exactly Till,
I always thought that a timeout was abnormal, and
who cares. I want a good Timeout mechanism, preferably
the one I posted here earlier, currently I don't throw an
exception in the Timedout() method, but set a bool 'timedout'
to true which I check in the while() loop. I don't think it's
particularly elegant, but it works.

Anybody with any ideas, without flaming some EXAMPLE code.

Laters! Wouter



Why NORMAL status?
He wants to throw a exception if his code runs into a timeout.
And he wants to catch this timeout-exception.

Till Meyer
[/QUOTE]
 
Hello Fred,

that's kind of what I built, except that I throw an
exception in WatchDogBark(). The problem is I can't catch
that exception. Currently I don't throw an exception but set
a bool indicating timeout. This bool can be checked in the loop
that does the action which can timeout (constructor PModel).
I do not think this method is particularly elegant,

Best regards, Wouter
 
Back
Top