Thanks Pete for your advise.
Here is what I want to do:
I have 2 classes "Scenario" and "Step", which have a
System.Thread.Timer for each to control their timeout gestion, in my
"main program", I start the "Scenario" and "Step", and I do something
between "StepStart" and "StepStop", when "Scenario" or "Step" timeout
expired, they raise an "exception", then my "main prog" receive the
exception, it will stop what it is doing and go into the catch bloc to
do anything else.
I tried pass the Thread.CurrentThread as object in System.Thread.Timer
thread to doing an Abort() later, I got the exception in my main
program catch bloc, but it crash my application later, so I have no
idea how solve my problem.
Here is the main class:
class TimerExample
{
[STAThread]
static void Main()
{
Thread scenThread = null;
try
{
// Class Scenario has a timeout 10s
Scenario scen = new Scenario(10000);
// start a step which has a timeout 3s
scen.StepStart(3000);
/*
do something here
*/
// for exemple: a thread sleep just for expired the step
timeout
Thread.Sleep(5000);
scen.StepStop()
scen.Stop()
Console.WriteLine("Fin Scenario");
}
catch (Exception ex)
{
//Here, for diffrent case, do diffrent things
Console.WriteLine(ex.Message);
}
}
}
//Here is class Scenario, which has start with a System.Thread.Timer
thread for doing the timeout
public class Scenario
{
private int scenTimeOut;
private Timer stateTimer;
private Step scenStep;
private int invokeCount;
public Scenario(int _scenTimeOut)
{
invokeCount = 0;
scenTimeOut = _scenTimeOut;
Console.WriteLine("Creating Scenario: " + scenTimeOut + "
ms");
TimerCallback timerDelegate = new
TimerCallback(maximumTimeExceeded);
stateTimer = new Timer(timerDelegate,
Thread.CurrentThread, _scenTimeOut, Timeout.Infinite);
}
void maximumTimeExceeded(Object obj)
{
Console.WriteLine("maximumTimeExceeded Scenario");
Stop();
//((Thread)obj).Abort();
throw new TimeoutException("Scenario Timeout")
}
public void StepStart(int _stepTimeOut)
{
scenStep = new Step(_stepTimeOut, ++invokeCount);
scenStep.Start();
}
public void StepStop()
{
scenStep.Stop();
}
public void Stop()
{
stateTimer.Dispose();
Console.WriteLine("Scenario Stop");
}
}
// Here is the Step class which has also a System.Thread.Timer for the
step timeout gestion
public class Step
{
public bool isRunning = false;
public int timeOut = 1000;
private Timer stateTimer;
private int stepIndex = 0;
public Step(int _timeOut, int _stepIndex)
{
timeOut = _timeOut;
stepIndex = _stepIndex;
}
void maximumTimeExceeded(Object obj)
{
if (isRunning)
{
Console.WriteLine("maximumTimeExceeded Step");
Stop();
//((Thread)obj).Abort();
throw new TimeoutException("Step Timeout")
}
}
public void Start()
{
isRunning = true;
TimerCallback timerDelegate = new
TimerCallback(maximumTimeExceeded);
Console.WriteLine("\nCreating Step " + stepIndex + " - " +
timeOut + " ms");
stateTimer = new Timer(timerDelegate,
Thread.CurrentThread, timeOut, Timeout.Infinite);
}
public void Stop()
{
if (isRunning)
isRunning = false;
stateTimer.Dispose();
Console.WriteLine("Step Stop");
}
}
Thanks
best regards
yeye