How to catch cross thread exceptioin

Y

yeye.yang

hey everybody
Does everybody can help me or give me some advise for the cross thread
exception catch

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
 
J

Jon Skeet [C# MVP]

Does everybody can help me or give me some advise for the cross thread
exception catch

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.

This sounds like a horrible abuse of exceptions. Also, aborting
threads is a really bad idea. You should implement a more orderly
shutdown.

See http://pobox.com/~skeet/csharp/threads/shutdown.shtml

Jon
 
Y

yeye.yang

Thanks Jon

I know thread.abort() is bad, but it is the only one I know to
generate a ThreadException and it return to the "main program",
unluckly, this exception raise again after catch bloc, that's why my
program crash, that is why I have the prob, do you have any good idea
for me to make the timeout gestion between the "main program" and a
"step thread"? something like "goto" in python or "jump" in c/c++, I
dont want to play with thread, all I want is catch the thread
exception from another.

thanks
best regards
yeye
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top