
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
1040
|
Chapter 18: Threading and Synchronization
be created through one of the AllocateDataSlot or AllocateNamedDataSlot methods
on the
Thread class.
To store data in this data slot, use the static
Thread.SetData method. This method
takes the object passed in to the
data parameter and stores it in the data slot defined
by the
dataSlot parameter.
The static
Thread.GetData method retrieves the object stored in a data slot. This
method retrieves a
LocalDataStoreSlot object that is created through the Thread.
GetNamedDataSlot
method. The GetData method then returns the object that was
stored in that particular data slot. Note that the object returned might have to be cast
to its original type before it can be used.
The static method
Thread.FreeNamedDataSlot will free the memory associated with a
named data slot. This method accepts the name of the data slot as a
string and, in
turn, frees the memory associated with that data slot. Remember that when a data
slot is created with
GetNamedDataSlot, a named data slot is also created on all of the
other threads running in that process. This is not really a problem when creating
data slots with the
GetNamedDataSlot method because, if a data slot exists with this
name, a
LocalDataStoreSlot object that refers to that data slot is returned, a new data
slot is not ...