c# - Loop runs too fast? -


i have loop (below) loops n number of times based on user input. loop, calls method creates random string of text insert database. want loop call method before executes query, every insert database has different random string of characters.

what seems happening, loop runs quickly, , random string inserted 50 times because dynamic string variable doesn't updated quick enough. however, if throw in thread.sleep(50), code executes perfectly.

i don't thread.sleep option because don't know how long needs sleep, , time add if start running few hundred thousand transactions. have solution ensuring method executes before moves on?

for (int = 0; < nloop; i++) {     rnd.rndname();     query.commandtext = "insert xxx (col";     query.executenonquery(); } 

what seems happening, loop runs quickly, , random string inserted 50 times because dynamic string variable doesn't updated quick enough .

the instructions inside loop executed 1 after other.

unless rnd.rndname() fires separate thread (in case, show code) complete before following 2 statements execute.

if name not changing, problem lies elsewhere.

however, if throw in thread.sleep(50), code executes perfectly.

nothing in code have shown sensitive thread sleep. if having effect, issue lies in how rnd.rndname() implemented. perhaps creating new instance of random each time (as suggested in comment @rynah)? if so, instance initialized using system time. cause behavior observe.

the random class not generate random numbers. generates deterministic series of numbers given seed value. if seed current number of ticks (which believe random does), creating many random instances in quick succession cause them have same seed, , therefore produce exact same sequence of numbers.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -