The capture variable occurs within the npage
declaration npage
. The npage
parameter npage
declared at the method level and does not change in this method - so using npage
is completely thread safe.
The problem that you are avoiding would occur if you change the variable in the declared scope, usually this is a loop - i.e.
for(int npage = 0; npage < 100 ; npage++) Foo( (s,e) => DoSomething(npage) ); // not safe; npage shared between all
however, breaking it down into a method that you avoid this, i.e.
for(int i = 0; i < 100; i++) Hit(i); ... void Hit(int npage) { Foo( (s,e) => DoSomething(npage) );
source share