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! 4 11109
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***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
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!
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*******@emai l.it> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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!
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 ManualResetEven t mResetEvent = new ManualResetEven t( 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.Res et();
}
/// <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 VerifySuspendSt op()
{
bool continueExecuti on = true;
TimeSpan timeout = new TimeSpan( 3, 0, 0 );
if ( !mResetEvent.Wa itOne( timeout, false ) )
{
mResults = "Timeout occured.";
continueExecuti on = false;
mExitStatus = ExitStatusType. TimeoutSuspend;
}
else if ( StopExecution )
{
mResults = "Thread stopped by user";
continueExecuti on = false;
mExitStatus = ExitStatusType. Stopped;
}
return continueExecuti on;
}
/// <summary>
/// Here I do what I want.
/// </summary>
protected void ExecControl()
{
while( VerifySuspendSt op )
{
// quick code
if ( !VerifySuspendS top )
{
return;
}
// time consuming code
if ( !VerifySuspendS top )
{
return;
}
// time consuming code
}
}
}
class ClassThatUsesEn gine
{
public ClassThatUsesEn gine()
{
Engine engine = new Engine();
Thread thread = new Thread( new ThreadStart( engine.Start ) );
thread.Start();
thread.Join();
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 work and then supends itself
(Thread.CurrentThread.Suspend()). When the user closes the MainForm window
the application does not quit since it has one thread that is not finished
(the one which is suspended).
What is the suggested way of...
|
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 I suspend and
resume whenver needed. In order to properly shut down the program every
thread has to be aborted. So, I override OnClosing(CancelEventArgs e) in my
main GUI program, and have it perform the following on the thread:
if...
|
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 delegate inside the UI that I call to update the progress meter.
I use the Suspend() and Abort() methods based on button events.
I can watch the progress meter increase just fine when the thread is
running. When I select Start, I enable the Cancel...
|
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 component becomes instabile, throwing a
Windows like error (access violation on 0x00000002), not an framework
exception. The component and all of its subcomponents are 100% managed code.
What could go wrong with Thread.Abort()?
Thanks for any hints.
|
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?
The sub procedure that "t" calls is an inifite loop with code inside. Near
the end of each interation, this sub() will call currentthread.sleep(x).
That works fine. I imagine the parent thread can't abort the child thread,
"t", until the...
| |
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()
However, this throws and error:
System.Threading.ThreadStateException: Thread is running or terminated; it
can not restart.
|
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 but using it under certain circumstances
still seems reasonable. For instance, I want to display a small modal dialog
with a "Cancel" button which allows the user to abort a background thread.
If the user clicks this button, I then want to prompt...
|
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
possibly "restart" it.
What've been doing is essentially:
if (SerialRowThread != null)
|
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 the
risk of changing the code everywhere so here is what I think I could do,
which is to create my own ThreadWrapper class which inherits from Thread and
which has these three methods already defined but does it a different way
and then my...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |