Solved

Problem with NamedDataSlot

Posted on 2006-06-16
5
344 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 Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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 …

861 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