Solved

Problem with NamedDataSlot

Posted on 2006-06-16
5
339 Views
Last Modified: 2008-01-09
I am trying to pass data to a thread using a NamedDataSlot. and I get an error.  Here is my code:

void StartThread()
        {
            int equipType = 45;
            button.Enabled = false;
            lock (stateLock)
            {
                target = Convert.ToInt32(txtTo.Text);
            }
            Thread t = new Thread(new ThreadStart(ThreadJob));
            t.IsBackground = true;

           
            // Pass thread the equipment type too look for
            Thread.AllocateNamedDataSlot("EquipType");
            LocalDataStoreSlot LDSSEquipType;
            LDSSEquipType = Thread.GetNamedDataSlot("EquipType");
            Thread.SetData(LDSSEquipType, equipType);

            t.Start();
        }

        void ThreadJob()
        {
            LocalDataStoreSlot LDSSEquipType;
            LDSSEquipType = Thread.GetNamedDataSlot("EquipType");
            if (!(LDSSEquipType == null))
            {
                int equipType = (int)Thread.GetData(LDSSEquipType);          //<--------------- ***************
            }
        }

On the line marked with *** I get a NullReferenceException error.  Can anyone tell me what I am doing wrong?  I've looked at a number of different sites to try to find out what I'm doing wrong and they are all doing the same way I am.

Thanks,
axnst2
0
Comment
Question by:axnst2
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:anyoneis
ID: 16923659
AXNST2;

"Data slots are unique per thread. '

so, you can't do what you are trying to do.

David
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 16923679
To expand on that (because I found it confusing), the call "Allocates a named data slot on all threads. " So each thread has the DataSlotName, but the contents are unique for each thread.

David
0
 

Author Comment

by:axnst2
ID: 16934037
I thought that what I was doing was consistent with what you're saying.  I allocate a Named Datat Slot in my main thread, assigned it with a value, and then I try to retreive that value in my ThreadJob thread.

Well, that's what I thought I was doing anyway.  Please let me know how I'm supposed to accomplish that.

Thanks,
axnst2
0
 
LVL 11

Accepted Solution

by:
anyoneis earned 500 total points
ID: 16935636
Think of named slots as being thread-specific globals:

"Threads and contexts use a local store memory mechanism to store thread-specific and context-specific data, respectively. The common language runtime allocates a multi-slot data store array to each process when it is created. The thread or context calls various functions to allocate a data slot in the data store, to store and retrieve a data value in the slot, and to free a data slot for reuse after the thread or context object expires.

The data slots are unique per thread or context; their values are not shared between the thread or context objects. Data slots can be allocated by a name or by an index number.
"

I don't have an answer for you on the best way to communicationthis kind of information. One thought would be to create a class which contained the worker method as well as the worker's data. The data would be set in the constructor.

David
0
 

Author Comment

by:axnst2
ID: 16942102
Thanks a lot for your help David.  I ended up just putting my thread in class of its own with properties, and I just create new instances of that class every time I need a new thread.  It's better programming practise anyway.  Of course, I will award you the points for the information.

Thanks,
axnst2
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Server Error 11 58
Reading the Web.Config using IIS 7.5? 4 36
Cant save 3D 4 20
application restarter  - for an exe that hangs 9 27
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question