i have following code:
dictionary<string, wsresponse> responsedictionary = new dictionary<string, wsresponse>(); list<task> tasklist = new list<task>(); foreach (string id in ids) { string localid = id; task newtask = task.factory.startnew(() => { wsresponse response = query.getlistfor(localid); responsedictionary.add(localid, response); }); tasklist.add(newtask); } task.waitall(tasklist.toarray());
on server code running, our team has server monitoring software, , told software reports unsafe static dictionary
being passed around. guess when use dictionary inside task, dictionary passed task thread unsafe pointer. on other hand, might have wrong. have tried find authoritative source talking this, failed. can explain runtime in case?
btw: swaping dictionary
concurrentdictionary
anyway, no need alarm.
how dictionary passed byref task
a dictionary reference type doesn't matter if passed by-value or by-ref. happens closure by-ref.
in code:
dictionary<string, wsresponse> responsedictionary = ...; task newtask = task.factory.startnew(() => { wsresponse response = query.getlistfor(localid); responsedictionary.add(localid, response); });
the dictionary declared outside scope of task lambda lambda use name. allowed , called 'closing over' variable.
and means multiple threads can access dictionary , should replaced concurrentdictionary. solve diagnostic message.
Comments
Post a Comment