[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

How to catch cross thread exceptioin

yeye.yang

10/2/2008 9:13:00 AM

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
14 Answers

Adam Benson

10/2/2008 9:26:00 AM

0

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.
=========

<yeye.yang@yahoo.fr> wrote in message
news:c4520cd5-d588-4ac1-b787-1b19b01c4887@d1g2000hsg.googlegroups.com...
> 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


yeye.yang

10/2/2008 10:01:00 AM

0

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

Peter Duniho

10/2/2008 10:44:00 AM

0

On Thu, 02 Oct 2008 03:00:46 -0700, <yeye.yang@yahoo.fr> wrote:

> 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?

Only if the "main program" has been coded to somehow receive information
from your thread. The "main program" being an individual thread itself.

I wouldn't agree that the AutoResetEvent or ManualResetEvent objects would
be the most likely candidates for this scenario. Those are generally used
for synchronization, not information-passing. But you do need to use
_some_ kind of object or data structure that can convey some information
from one thread to another.

What exactly to use isn't possible to say without knowing what your main
thread is doing. It just depends. For example, in the case of certain
kinds of i/o operations, one way to manage a timeout is simply to close
the i/o object from another thread when the timeout occurs. Then, if an
i/o operation is pending on the main thread, that operation will throw an
exception.

An alternative approach would be to simply have the main thread check for
some state that's managed by the other thread, either throwing your
"timout exception" when the state is set to some appropriate value, or
simply handling the condition as if an exception had been thrown
(depending on what works best for your design).

There are in fact ways to generate exceptions in one thread from another,
but they are all dependent on the specific scenario. Without knowing what
your specific scenario is, it's not possible to say what might be the
right approach for you.

Pete

yeye.yang

10/2/2008 12:54:00 PM

0

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

Peter Duniho

10/2/2008 6:04:00 PM

0

On Thu, 02 Oct 2008 05:54:05 -0700, <yeye.yang@yahoo.fr> wrote:

> 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.

Well, in the code you posted, the most obvious issue is that after
catching the exception, you simply exit the application. I don't know if
that's what you mean by "it crash my application later", but the code
posted definitely doesn't do anything useful with the exception.

Your sample code also does not show what sort of operation is actually
going on when you want to interrupt it, so it's impossible to provide
details as to what the best way to do that would be. As I mentioned
before, different kinds of operations have different techniques that are
most useful, so without knowing what the operation is, a good answer
cannot be provided.

What I can tell you is that using Thread.Abort() is fragile and very
dependent on what's actually going on. There are numerous caveats with
respect to the use of Thread.Abort() (documented on MSDN), any of which
could interfere with your ability to get it to work as expected. There
are other inter-thread communications techniques that are almost certainly
more appropriate.

Pete

yeye.yang

10/2/2008 11:09:00 PM

0

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

Peter Duniho

10/2/2008 11:20:00 PM

0

On Thu, 02 Oct 2008 16:08:42 -0700, microsoft.public.dotnet.framework
<yeye.yang@yahoo.fr> wrote:

> [...]
> 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,

Other than your use of the non-word "gestion", your post seems reasonably
understandable. I don't think I'm having trouble with your use of
English. But you continue to avoid the one question that I've said needs
answering in order to provide any useful advice: what is the main thread
doing when you want to interrupt it?

Every code sample you've posted, whether C# or pseudocode, completely
glosses over that question. You even went so far as to write "....."
instead of actually answering the question in your most recent post.

Until you provide that level of detail, no equivalent level of detail in
an answer will be possible.

Pete

yeye.yang

10/3/2008 7:52:00 AM

0

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

Mike Blake-Knox

10/4/2008 1:53:00 PM

0

In article
<39831208-c89c-41cf-b496-1ad4f874b6b9@t41g2000hsc.googlegroups.com>,
Microsoft.public.dotnet.framework wrote:
> 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.

In an "ordinary" C# Windows Forms application you catch exceptions that
are not handled by other threads though the use of an Unhandled
Exception Event Handler. This might be setup through code like:

AppDomain.CurrentDomain.UnhandledException += // CLR
new UnhandledExceptionEventHandler(OnUnhandledException);

I don't know how this would fit into your Excel automation environment.
(Were you aware that you can use Excel's macro recorder to record a
sequence then edit the result VBA code to customize the behaviour?)

I'm afraid I'm curious about the word gestion that you've used a few
times. What do you mean by it.

Mike


~Z~

10/4/2008 3:04:00 PM

0

microsoft.public.dotnet.framework wrote:
> 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?