How to catch cross thread exceptioin

  • Thread starter Thread starter yeye.yang
  • Start date Start date
Y

yeye.yang

hey everybody,

Sorry for my poor english first,
My program is under .NET (C#), I have a main program, then I got a
thread (System.Threading.Timer) at the same time for control a timeout
for the main program. When my thread timeout expired, I raise an
exceptioin "Timeout" in my thread, my main program should be catching
the exception and do some code in catch bloc. In fact I can't catch
the exception from the thread.

Can anyone help me or give me some advise for this?

Thanks
Best regards

yeye
 
You can only catch an exception in the thread that raised it.

If you want to signal an event from one thread to another use
System.Threading.AutoResetEvent or System.Threading.ManualResetEvent

Kind Regards,

Adam.
=========
 
Thanks Adam for your replay.

In fact, what I want to do is when the event will be signaled from my
thread, my main program stop what it is doing then do something else,
such like "go to" or "jump" in C/C++, is this possible?

thanks
best regards

yeye
 
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
 
Thanks Pete
Sorry for the cross-post.

Well, what I plan to do is to create a "Scenario" and "Scenario Step"
gestion,
main()
{
try
{
start scenario (thread.timer with a scenario timeout)
start 1st step (thread.timer with a step timeout)
.....
.....
etc ...
stop 1st step
start 2nd step
.....
.....
etc ...
stop 2nd step
stop scenario
}
catch
{
if (scenario timeout)
....... write timeout log
else if (step timeout)
....... write timeout log
}
exit
}

It should be suspend the main program when it receive a scenario or
step timeout event, and all I found to suspend the main program is
generate an exception to put it into the catch bloc directly.
For my poor english, I dont know wether you understand me, I hope so,

Thanks
best regards

yeye
 
Thanks very much Pete

Well, in fact, I am doing a program which automate excel, here is the
playback scenario which should be an exe.
Between every "stepstart" and "stepstop", I automate excel's UI
actions, that is the reason I want to setup a step timeout for limit
the user time even though all actions in step are not finish, it
should be "cut it out" and do write a log error or something like that
for finish the program.
For doing this, first thing I thought is a "try catch" bloc, and
thread gestion for "step", when timeout expired, jump direct into
catch bloc.
And the "main program" that I mentioned is all the code line in "try"
bloc.

Thanks
best regards

yeye
 
microsoft.public.dotnet.framework said:
main()
{
try
{
start scenario (thread.timer with a scenario timeout)
start 1st step (thread.timer with a step timeout)
.....
.....
etc ...
stop 1st step
start 2nd step
.....
.....
etc ...
stop 2nd step
stop scenario
}
catch
{
if (scenario timeout)
....... write timeout log
else if (step timeout)
....... write timeout log
}
exit
}

It looks like you're trying to use a timer to tell when an operation has
taken too long to complete.

Is that correct?
 
Hey Mike Blake-Knox
Your are right, that is what I want to do, using a timer to limit the
execution time. So have you an idea for catch cross thread in c#
console mode?

Thanks
Best Regards

yeye
 
Back
Top