473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to catch cross thread exceptioin

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
Oct 2 '08 #1
3 3158
On Oct 2, 2:00*pm, yeye.y...@yahoo.fr wrote:
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
Oct 2 '08 #2
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
Oct 2 '08 #3
On Thu, 02 Oct 2008 06:00:06 -0700, <ye*******@yahoo.frwrote:
hey everybody
Does everybody can help me or give me some advise for the cross thread
exception catch
Please don't multi-post. If you feel you must post the same question to
multiple newsgroups, learn to cross-post correctly.

I've responded to this identical question in the m.p.dotnet.framework
newsgroup.
Oct 2 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
23
by: Jeff Rodriguez | last post by:
Here's what I want do: Have a main daemon which starts up several threads in a Boss-Queue structure. From those threads, I want them all to sit and watch a queue. Once an entry goes into the...
3
by: jlamanna | last post by:
I was wondering if there was a utility that could tell you when your C# application is making cross-apartment COM calls. I have a fairly large application that makes extensive use of a 3rd party...
6
by: foolmelon | last post by:
If a childThread is in the middle of a catch block and handling an exception caught, the main thread calls childThread.Abort(). At that time a ThreadAbortException is thrown in the childThread. ...
1
by: batvanio | last post by:
Hi, I am writing unit tests in VS2005 and am having the following problem: I am trying to test a timeout property of one of my methods. This timeout exhibits itself in an exceptioin - i.e. I am...
8
by: Rohit Kumbhar | last post by:
Hi, I am writing a Windows Forms C# application for reading files on the disk. My problem is the UI freezes when restored after being minimized for a long time . I have a separate thread...
5
by: nospam | last post by:
Hi all. I have encountered a "Cross-thread operation not valid" error in a test program I created and although I have came up with a solution I don't like the performance of the program. I...
28
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
3
by: kimiraikkonen | last post by:
Hi, I was looking for an example on CodeProject and saw an interesting thing, i downloaded the article source code and converted to my VB 2005 Express and compiled with no problem, however when i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.