c# - Accessing Thread Local Storage -


when 2nd thread executes, results in exception. can pls explain why?

class tls {     public void run()     {         lock (this)         {             console.writeline(thread.currentthread.managedthreadid + " started.");             localdatastoreslot ldss = thread.allocatenameddataslot("unique"); // exception             thread.setdata(ldss, "some_data");             string = thread.getdata(ldss) string;             thread.sleep(1000);             console.writeline(thread.currentthread.managedthreadid + " ended.");         }     } } 

exception details:

at system.collections.hashtable.insert(object key, object nvalue, boolean add) @ system.localdatastoremgr.allocatenameddataslot(string name) @ consoleapplication2.tls.run() in autolock.cs:line 65 @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.threading.threadhelper.threadstart()

thanks.

you trying allocate slot same name twice. might want have read on msdn documentation.

update: should allocate slot once - before start threads. in main program. right doing everytime thread starts , that's why getting exception.


Comments