473,382 Members | 1,512 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()

I will do several things in my thread:
Copy a file to a location
Update database record
Read the file content
Write the content to a log file

If I call Thread.Abort(), it may be possible to stop at the middle of the
something ?
Oct 18 '06 #1
5 2778
Hi Alan,

Suspending and then Aborting a thread may cause data integrity problems if
your thread in middle of updating some business data. And anyway these two
methods are deprecated in .Net 2.0.

If your thread is doing some routine data related manipulations in a loop
(or some loop kind of thing), then implement a small class that wraps your
thread and provide "Pause", "Resume" and "Stop" methods on that class. Define
your thread handler method as a method inside the class and based on the
state of the class object ("Pause", "Resume" and "Stop") let the thread
handler method pause, resume and stop by itself.

This way you gurantee the data safety in your application and also provide a
decent way of pausing, resuming and stopping the work done by your thread.

--
Regards,
Aditya.P
"Alan T" wrote:
I will do several things in my thread:
Copy a file to a location
Update database record
Read the file content
Write the content to a log file

If I call Thread.Abort(), it may be possible to stop at the middle of the
something ?
Oct 18 '06 #2
I partially understand your approach but not quite sure how to implement the
stop and resume.

The syntax is not proper, I just show the meaning:

Class MyThreadWrapper
private _myThread
private enumerated type _state (pause, stop, start, resume)
method Pause
{ set _state = pause}
method Stop
{ set _state = stop}
method Start
{ set _state = start
_myThread.Start()
}
method Resume
{ set _state = resume}

If the user calls MyThreadWrapper.State(),
_state will be set to start and calls
constructor :
_myThread = new Thread(new ThreadStart(this.DoSomething));

In the _myThread, it executes DoSomething(), to do a loop, for example 50
times:

procedure DoSomething()
loop
Copy a file to a location
Update database record
Read the file content
Write the content to a log file
end loop

1) If the user calls MyThreadWrapper.Start()

2) If the user calls MyThreadWrapper.Pause(), how do I guarrantee the loop
stops at after the
"Write the content to a log file" ? By checking the value of the _state?

procedure DoSomething()
loop
Copy a file to a location
Update database record
Read the file content
Write the content to a log file
while _state == pause
{
// no statement, so stay in this dummy while loop
}
end loop

2) What if the users calls MyThreadWrapper.Stop() ?
procedure DoSomething()
loop
Copy a file to a location
Update database record
Read the file content
Write the content to a log file
while _state == pause
{
// no statement
}
if _state == stop
{
exit;
}
end loop

Should it be like this:

procedure Stop()
{
_state = stop;
_myThread.abort();
}

Please comment

"Adityanand Pasumarthi" <Ad******************@discussions.microsoft.com>
wrote in message news:55**********************************@microsof t.com...
Hi Alan,

Suspending and then Aborting a thread may cause data integrity problems if
your thread in middle of updating some business data. And anyway these two
methods are deprecated in .Net 2.0.

If your thread is doing some routine data related manipulations in a loop
(or some loop kind of thing), then implement a small class that wraps your
thread and provide "Pause", "Resume" and "Stop" methods on that class.
Define
your thread handler method as a method inside the class and based on the
state of the class object ("Pause", "Resume" and "Stop") let the thread
handler method pause, resume and stop by itself.

This way you gurantee the data safety in your application and also provide
a
decent way of pausing, resuming and stopping the work done by your thread.

--
Regards,
Aditya.P
"Alan T" wrote:
>I will do several things in my thread:
Copy a file to a location
Update database record
Read the file content
Write the content to a log file

If I call Thread.Abort(), it may be possible to stop at the middle of the
something ?

Oct 19 '06 #3
Hi Alan,

Here is how your class and thread method handler maylook like...

Class MyThreadWrapper
private _myThread

// (Important: Default state should be signaled, i.e. True)
private _continueEvent System.Threading.ManualResetEvent

private enumerated type _state (pause, stop, start, resume)
method Pause
{ set _state = pause, _continueEvent.Reset() }
method Stop
{ set _state = stop, _continueEvent.Set() }
method Start
{ set _state = start
_myThread.Start()
}
method Resume
{ set _state = resume, _continueEvent.Set() }

procedure DoSomething()
loop

_continueEvent.Wait(-1) // Wait infinitely on the manual reset event
if (_state == stop) { break; }

Copy a file to a location
Update database record
Read the file content
Write the content to a log file
end loop

What we are doing here is that the thread loop will just wait for
ManualResetEvent to be in signaled state (_continueEvent.Set()) before
continuing with every iteration in the loop. The event will be in this state
when the thread first starts. When we call Pause the_continueEvent will be in
non-signaled state and the thread will finish the current loop iteration and
then before begining the next iteration will wait for the event to be in
signaled state. When we call Resume after some time the _continueEvent will
be set to signaled state and the thread will start running again.

Calling Stop() when we are in running state (start or resume) will cause the
loop to terminate after completing its current iteration. Calling Stop() when
we are in pause state will set the _continueEvent to signaled state and
immediately since the _state is stop, the loop will break and the thread will
end safely.

Let me know if this helps.

--
Regards,
Aditya.P
"Alan T" wrote:
I partially understand your approach but not quite sure how to implement the
stop and resume.

The syntax is not proper, I just show the meaning:

Class MyThreadWrapper
private _myThread
private enumerated type _state (pause, stop, start, resume)
method Pause
{ set _state = pause}
method Stop
{ set _state = stop}
method Start
{ set _state = start
_myThread.Start()
}
method Resume
{ set _state = resume}

If the user calls MyThreadWrapper.State(),
_state will be set to start and calls
constructor :
_myThread = new Thread(new ThreadStart(this.DoSomething));

In the _myThread, it executes DoSomething(), to do a loop, for example 50
times:

procedure DoSomething()
loop
Copy a file to a location
Update database record
Read the file content
Write the content to a log file
end loop

1) If the user calls MyThreadWrapper.Start()

2) If the user calls MyThreadWrapper.Pause(), how do I guarrantee the loop
stops at after the
"Write the content to a log file" ? By checking the value of the _state?

procedure DoSomething()
loop
Copy a file to a location
Update database record
Read the file content
Write the content to a log file
while _state == pause
{
// no statement, so stay in this dummy while loop
}
end loop

2) What if the users calls MyThreadWrapper.Stop() ?
procedure DoSomething()
loop
Copy a file to a location
Update database record
Read the file content
Write the content to a log file
while _state == pause
{
// no statement
}
if _state == stop
{
exit;
}
end loop

Should it be like this:

procedure Stop()
{
_state = stop;
_myThread.abort();
}

Please comment

"Adityanand Pasumarthi" <Ad******************@discussions.microsoft.com>
wrote in message news:55**********************************@microsof t.com...
Hi Alan,

Suspending and then Aborting a thread may cause data integrity problems if
your thread in middle of updating some business data. And anyway these two
methods are deprecated in .Net 2.0.

If your thread is doing some routine data related manipulations in a loop
(or some loop kind of thing), then implement a small class that wraps your
thread and provide "Pause", "Resume" and "Stop" methods on that class.
Define
your thread handler method as a method inside the class and based on the
state of the class object ("Pause", "Resume" and "Stop") let the thread
handler method pause, resume and stop by itself.

This way you gurantee the data safety in your application and also provide
a
decent way of pausing, resuming and stopping the work done by your thread.

--
Regards,
Aditya.P
"Alan T" wrote:
I will do several things in my thread:
Copy a file to a location
Update database record
Read the file content
Write the content to a log file

If I call Thread.Abort(), it may be possible to stop at the middle of the
something ?


Oct 19 '06 #4
Hi !

I just went to http://msdn2.microsoft.com/en-us/library/ty8d3wta.aspx to
verify your statement about the deprecation of this methods, but could
not verify.

Additionally, I would leave net immidiately;For example, if your
mentioned "copy a file to a location", would need too much time, because
the underlying system is not resposnible, I must have the option to kill
that thread and re-try the whole operation at a later moment. Naturally,
one has to deal with dataconsitency in this case very strong.

Just my two cents.
Best regards,
Manfred

Adityanand Pasumarthi wrote:
Hi Alan,

Here is how your class and thread method handler maylook like...

Class MyThreadWrapper
private _myThread

// (Important: Default state should be signaled, i.e. True)
private _continueEvent System.Threading.ManualResetEvent

private enumerated type _state (pause, stop, start, resume)
method Pause
{ set _state = pause, _continueEvent.Reset() }
method Stop
{ set _state = stop, _continueEvent.Set() }
method Start
{ set _state = start
_myThread.Start()
}
method Resume
{ set _state = resume, _continueEvent.Set() }

procedure DoSomething()
loop

_continueEvent.Wait(-1) // Wait infinitely on the manual reset event
if (_state == stop) { break; }

Copy a file to a location
Update database record
Read the file content
Write the content to a log file
end loop

What we are doing here is that the thread loop will just wait for
ManualResetEvent to be in signaled state (_continueEvent.Set()) before
continuing with every iteration in the loop. The event will be in this state
when the thread first starts. When we call Pause the_continueEvent will be in
non-signaled state and the thread will finish the current loop iteration and
then before begining the next iteration will wait for the event to be in
signaled state. When we call Resume after some time the _continueEvent will
be set to signaled state and the thread will start running again.

Calling Stop() when we are in running state (start or resume) will cause the
loop to terminate after completing its current iteration. Calling Stop() when
we are in pause state will set the _continueEvent to signaled state and
immediately since the _state is stop, the loop will break and the thread will
end safely.

Let me know if this helps.
Oct 19 '06 #5

"mabra" <mabra@homewrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
| Hi !
|
| I just went to http://msdn2.microsoft.com/en-us/library/ty8d3wta.aspx to
| verify your statement about the deprecation of this methods, but could
| not verify.
|

Thread.Abort isn't deprecated but Thread.Suspend and Thread.Resume are.
Note that Thread.Abort should never be used to abort an other thread unless
you are willing to give up on the application domain your code is running
in, there are better way's to stop a thread from making progress, and don't
forget that Thread.Abort doesn't have any effect on a thread that is
executing in unmanaged land (like copy file).

Willy.

Oct 20 '06 #6

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

Similar topics

14
by: Daisy | last post by:
From this page: http://www.c-sharpcorner.com/2/mt_beginner1.asp Thread class's Abort method is called to kill a thread permanently. Make sure you call IsAlive before Abort. if (...
7
by: Morris | last post by:
I want to abort a running thread, so I call MyThread.abort() function. My problem is this thread runs "almost" like a while(true) loop and I don't want the Abort() function interrupts the thread at...
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...
1
by: benmorganpowell | last post by:
I have a small windows service which connects to a POP3 server at defined intervals, scans the available messages, extracts the required information and inserts the data into a SQL database. I am...
2
by: Mark Denardo | last post by:
I'm trying to abort a suspended thread, but I get a ThreadStateException: An unhandled exception of type 'System.Threading.ThreadStateException' occurred in mscorlib.dll Additional...
6
by: Joe HM | last post by:
Hello - I have a function that calls Thread.Abort() to stop a thread in a _Closed() Method of a GUI. The thread contains a blocking call on a TCP socket and that is the easiest way to stop...
23
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() {...
5
by: andrew | last post by:
Hi, I have the following issue with the Thread.Abort(): The main thread creates a worker thread which waits on a process termination. void ThreadProc() { Process proc =...
6
by: mehdi | last post by:
Hi folks, You know, the Thread class has got a method named Abort which according to the msdn: "Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.