Solved

Problem with NamedDataSlot

Posted on 2006-06-16
5
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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