Solved

Problem with NamedDataSlot

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

21 Experts available now in Live!

Get 1:1 Help Now