c# - ThreadPool.QueueUserWorkItem inside foreach use same dataset -



c# - ThreadPool.QueueUserWorkItem inside foreach use same dataset -

in function below same user object passed dorestcall method

(i have logging in dorestcall method , has same first info in user object) need utilize parallel.foreach instead of threadpool

private void createuser(dataservicecollection<user> epusers) { foreach (user user in epusers) { seek { threadpool.queueuserworkitem(new waitcallback(f => { dorestcall(string.format("message-type=userenrollmentcreate&payload={0}", genaraterequestuserdata(user)), true); })); } grab (exception ex) { _logger.error("error in createuser " + ex.message); } } }

the problem how loop variables handled when used in lambda look or anonymous methods. lambda look sees current value of loop variable @ time lambda executed. believe behaviour changed in c# 5.0, haven't tried yet.

you need store current user in variable within foreach loop , utilize instead of loop variable (also, seek / grab doesn't grab exceptions within waitcallback, see prepare below):

foreach (user user in epusers) { user currentuser = user; threadpool.queueuserworkitem(new waitcallback(f => { seek { dorestcall(string.format("message-type=userenrollmentcreate&payload={0}", genaraterequestuserdata(currentuser)), true); } grab (exception ex) { _logger.error("error in createuser " + ex.message); } })); }

c# multithreading threadpool parallel.foreach

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -