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