Not just changing thread parameters on the fly (when the thread is started) but also passing parameters for start can be effectively solved by using a simple thread wrapper:
internal class ThreadWrapper {
internal ThreadWrapper() {
Thread = new System.Threading.Thread(Body);
}
internal void Start() { this.Thread.Start(); }
internal void Abort() { this.Thread.Abort(); }
internal int Parameter {
get { lock(lockObject) return fParameter; }
set { lock(locjObject) fParameter = value; }
}
void Body() {
try {
while (true) {
int parameter;
lock(locjObject) { parameter = fParameter ;}
}
} catch (ThreadAbortExeption e) {
finally {
}
object lockObject = new object();
int fParameter;
}
The key here is making the whole instance of the wrapper accessible by the wrapped thread via the implicit "this" parameter which was passed to thread constructor by passing it the non-static method (instance method)
Body
.
Whatever you do, remember that if you read and/or write any data from/to the wrapper in different threads (one of them could be a wrapped thread, but this is not required) you need to guard the shared memory with
lock
or
System.Threading.ReaderWriterLockSlim
, see
http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx[
^].
—SA