multithreading - Simple LOCK and ThreadPoolQuestion for WP7 (C#) -



funny, going test tombstoning , concurrency, , during simple setup, thought heck, worth trouble. now, after 45 minutes of setting stupid test classes, ran first error don't understand.

seems need little bit more practice in lists, locks , threads. know why throws illegal operation exception (see attached code).

for 1 likes f5 experience better, here complete solution (300kb)
http://www.filesavr.com/txxxfve40gtjk43

do not open views, might crash vs2010. , need wp7 tools, sorry, though pretty sure example work (not work) on pure c# also.

[edit] update link, working (with old code)
, found first bug, comment.

this works:

    private void inconewithlock()     {         lock (counterlistone)         {             inclistone();         }     }      private void inclistone()     {         if (counterlistone == null)         {             log("counterlistone == null");             return;         }          var c = 0;         var oldlist = counterlistone.tolist();         foreach (var in oldlist)         {             counterlistone[c++] = + 1;             thread.sleep(next(80*delayfactor, 150*delayfactor));         }     } 

will keep testing tombstoning stuff , post possible questions in later thread. changing list while iterating - hello newbie mistake :-)

[/edit]

for conveniece, expection occurs in function, , invalid operation expection:

 private void inconewithlock()     {         if (counterlistone == null)         {             log("counterlistone == null");             return;         }          lock (this)         {             var c = 0;             foreach (var in counterlistone)             {                 counterlistone[c++] = + 1;                 thread.sleep(next(80 * delayfactor, 150 * delayfactor));             }         }     } 

here full source of test class:

public class counterclass : testbase {     private dispatchertimer _dt;     public int countera { get; set; }      public observablecollection<int> counterlistone { get; set; }     public list<int> counterlisttwo { get; set; }     public list<int> counterlistthree { get; set; }     private const int delayfactor = 10;       public counterclass()     {         counterlistone = new observablecollection<int>();         counterlisttwo = new list<int>();         counterlistthree = new list<int>();          initcounterlists();         startbackgroundlogger();     }      public void loglists()     {         lock (this)             //lock (counterlisttwo)              //   lock (counterlistthree)                 {                     log("====================================================");                     log("counterlistone   " + string.join("-", counterlistone.select(x => x.tostring()).toarray()));                     log("counterlisttwo   " + string.join("-", counterlisttwo.select(x => x.tostring()).toarray()));                     log("counterlistthree " + string.join("-", counterlistthree.select(x => x.tostring()).toarray()));                     log("====================================================");                 }     }      public void runtests()     {         log("multiincwithoutlocks");         //multiincwithoutlocks();          log("multiincwithlocks");         multiincwithlocks();     }      public void multiincwithoutlocks()     {         threadpool.queueuserworkitem(x => inconewithoutlock());         threadpool.queueuserworkitem(x => inconewithoutlock());         threadpool.queueuserworkitem(x => inconewithoutlock());         threadpool.queueuserworkitem(x => inconewithoutlock());         threadpool.queueuserworkitem(x => inconewithoutlock());     }      public void multiincwithlocks()     {         threadpool.queueuserworkitem(x => inconewithlock());         threadpool.queueuserworkitem(x => inconewithlock());         threadpool.queueuserworkitem(x => inconewithlock());         threadpool.queueuserworkitem(x => inconewithlock());         threadpool.queueuserworkitem(x => inconewithlock());     }      private void inconewithoutlock()     {         var c = 0;         foreach (var in counterlistone)         {             counterlistone[c++] = i+1;             thread.sleep(next(80 * delayfactor, 150 * delayfactor));         }     }      private void inconewithlock()     {         if (counterlistone == null)         {             log("counterlistone == null");             return;         }          lock (this)         {             var c = 0;             foreach (var in counterlistone)             {                 counterlistone[c++] = + 1;                 thread.sleep(next(80 * delayfactor, 150 * delayfactor));             }         }     }      private void initcounterlists()     {         initcounterone();         initcountertwo();         initcounterthree();     }       private void initcounterone()     {         (int = 0; < next(1, 5); i++)         {             counterlistone.add(0);         }     }      private void initcountertwo()     {         (int = 0; < next(1, 5); i++)         {             counterlisttwo.add(0);         }     }      private void initcounterthree()     {         (int = 0; < next(1, 5); i++)         {             counterlistthree.add(0);         }     }      private void startbackgroundlogger()     {          _dt = new dispatchertimer();          _dt.tick += (a,b) => loglists();         _dt.interval = new timespan(0,0,0,3);         _dt.start();     }   } 

you should provide more detailed description of exception. connected foreach , counterlistone: changed while iterating on values, results invalidoperationexception.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -