Re: Thread.Abort()



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" <AdityanandPasumarthi@xxxxxxxxxxxxxxxxxxxxxxxxx>
wrote in message news:55E4782D-DE73-4EAB-841F-A481AC28039A@xxxxxxxxxxxxxxxx
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 ?





.



Relevant Pages

  • Re: Thread.Abort()
    ... private _myThread ... private _continueEvent System.Threading.ManualResetEvent ... private enumerated type _state (pause, stop, start, resume) ... end loop ...
    (microsoft.public.dotnet.languages.csharp)
  • Variable/Field value not being held
    ... A for-next loop is not the equivalent of a pause. ... >back into a field on the first form ~ Code runs through ... >yet then how could the calculation take place ~ not so, ...
    (microsoft.public.access.forms)
  • Re: Pause in a while loop
    ... The while loop inside ... consists of a custom-made function to play a sound, like, wavplay, ... After these 2 parts, I have added a pause command, so that I ...
    (comp.soft-sys.matlab)
  • Re: Number of pause seconds is out of range
    ... Pause using the JOB to wait for the job to finish, ... created and the WHILE loop would exit when more than 20 CPU seconds expire. ... I'm running a stream job that streams other jobs. ... * To join/leave the list, search archives, change list settings, * ...
    (comp.sys.hp.mpe)
  • Re: Parsing the appended data of log file in real time
    ... When peek doesnt return any data then sit in an idle loop till it does.. ... Actually another application is writing to the log file. ... 'scriptInAction is the control vairiable for the do util loop while ... currentLine = logFile.ReadLine ...
    (microsoft.public.dotnet.languages.vb)