473,382 Members | 1,368 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,382 software developers and data experts.

thread.abort and thread.suspend

am
Hi to all.
I have a little problem. I'm working with threads, and I need to abort
or suspend them, but many experts dissuade from use Thread.Abort and
Thread.Suspend. As I didn't find other way, how can I do?
Thanks a lot!

Nov 17 '05 #1
4 11079
am wrote:
Hi to all.
I have a little problem. I'm working with threads, and I need to abort
or suspend them, but many experts dissuade from use Thread.Abort and
Thread.Suspend. As I didn't find other way, how can I do?
Thanks a lot!


Usually you're suspending the thread in order to wait for something to
do and to do that you're better off using one of the synchronizing
objects (like the event objects) and wait for the events to become
signalled.

As for aborting a thread, that has a whole host of problems associated
with it (with Thread.Abort that is) and a better way is to look at an
event object to become signalled and then simply return from the thread
method.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #2
hi,

i had this issue with aborting thread:
my separate thread was generating bitmap thumbnails from larger bimap
files. when i was aborting this thread it sometimes happened that bitmap
objects were not disposed immediately, so i ended up with 200-300 megs
of memory ussage.
someone suggested that i should create some sort of stop signal value
which is frequently checked by the thread. if the value is set to true
then thread skips all its job and exits.
this works well but has one problem - if operation performed by thread
is very time consuming, like reading or writing large files or
resampling bimaps *AND* this is done in one line of code, then you cant
cancel this operations with metioned stop signal. so you may end up
waiting for the operation to finish. offcourse with stop signal you end
up with clean exit (you can free all resources), but you cant cancel a
single operation this way.
and for that i didn't found a sollution :(

am wrote:
Hi to all.
I have a little problem. I'm working with threads, and I need to abort
or suspend them, but many experts dissuade from use Thread.Abort and
Thread.Suspend. As I didn't find other way, how can I do?
Thanks a lot!

Nov 17 '05 #3
Hi,

Take a look at Skeet's article :
http://www.yoda.arachsys.com/csharp/threads/abort.shtml

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"am" <am*******@email.it> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi to all.
I have a little problem. I'm working with threads, and I need to abort
or suspend them, but many experts dissuade from use Thread.Abort and
Thread.Suspend. As I didn't find other way, how can I do?
Thanks a lot!

Nov 17 '05 #4
am
Thanks a lot to all!
Now I will post my solution. If you find something wrong tell me
please. Thanks a lot again.

class Engine
{

private ManualResetEvent mResetEvent = new ManualResetEvent( true );
private volatile bool mStopExecution;

protected bool StopExecution
{
get { return mStopExecution; }
}

/// <summary>
/// Stops as soon as possible
/// </summary>
protected internal void Stop()
{
mStopExecution = true;

// restart thread in case it was suspended before stop request
Restart();
}

/// <summary>
/// Suspends thread
/// </summary>
protected internal void Suspend()
{
// set the monitor to not segnaled if thread is not requested to stop
if ( !StopExecution )
mResetEvent.Reset();
}

/// <summary>
/// Restart suspended thread
/// </summary>
protected internal void Restart()
{
// segnales monitor
mResetEvent.Set();
}

/// <summary>
/// This is the function used as argument of new ThreadStart() in the
constractor of
/// new Thread
/// </summary>
protected internal void Start( )
{
try
{
ExecControl();
}
catch ( Exception ecc )
{
mExitStatus = ExitStatusType.Errors;
mError = ecc;
}
finally
{
Done();
}
}

/// <summary>
/// Looks for suspend or stop requests.
/// </summary>
protected bool VerifySuspendStop()
{
bool continueExecution = true;

TimeSpan timeout = new TimeSpan( 3, 0, 0 );

if ( !mResetEvent.WaitOne( timeout, false ) )
{
mResults = "Timeout occured.";
continueExecution = false;
mExitStatus = ExitStatusType.TimeoutSuspend;
}
else if ( StopExecution )
{
mResults = "Thread stopped by user";
continueExecution = false;
mExitStatus = ExitStatusType.Stopped;
}

return continueExecution;
}

/// <summary>
/// Here I do what I want.
/// </summary>
protected void ExecControl()
{
while( VerifySuspendStop )
{
// quick code

if ( !VerifySuspendStop )
{
return;
}

// time consuming code

if ( !VerifySuspendStop )
{
return;
}

// time consuming code
}
}

}

class ClassThatUsesEngine
{
public ClassThatUsesEngine()
{
Engine engine = new Engine();

Thread thread = new Thread( new ThreadStart( engine.Start ) );

thread.Start();

thread.Join();
}
}

Nov 17 '05 #5

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

Similar topics

4
by: Guido Kraus | last post by:
Hi, I have a VB Windows Forms app that has a single form ('MainForm'). MainForm has a shared (C#: static) class variable that holds a reference to a newly created Thread. This thread does some...
4
by: Stephan Steiner | last post by:
Hi I'm having some weird threading issues.. almost at random, if I dare change a line of my code, the shutdown sequence gets messed up. I'm using a thread to receive data from the network, that...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
10
by: Brett | last post by:
I have a second thread in which I call t.abort() from the parent thread. I see "t"s state as AbortRequested. It stays this way for some time. Under which conditions will a thread remain this way? ...
22
by: Brett | last post by:
I have a second thread, t2, that errors out and will stop. It's status is then "Stopped". I try to start t2 from thread 1, t1, by checking If t2.threadstate = "Stopped" Then t2.start() ...
6
by: Robert Speck | last post by:
Hi there, Can anyone shed anymore light on why "Thread.Suspend()" has been deprecated by MSFT beyond what MSDN says about it. I'm not sure if I quite appreciate the various pitfalls they discuss...
10
by: Jon Slaughter | last post by:
Since a thread doesn't have a Stop feature and I'm not supose to use abort, I'm wondering how I stop a thread? My problem is that I simply want to excute a function in the background and...
6
by: Buddy Home | last post by:
Hello, I want to understand whats the best way to write code to replace Thread.Suspend, Thread.Resume and Thread.Abort. I have lots of code calling these existing methods and want to minimize...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.