Solved

Problem with NamedDataSlot

Posted on 2006-06-16
5
329 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now