c# - How I implement Multithread pool for a repetitive process -
i need test() calculations, want put cpu core @ work find solution faster.
for (int i= 0; i<1000000; i++) { result[i] = test(i); if (result[i] == 0) { break; } }
i have work backgroundworker before. , create array of n bgworker , handle queue myself looks trouble.
so found task.factory, seem similar want, still don't know how handle each separate task wait result , stop when found asnwer.
task<string> task = task.factory.startnew<string> (() => downloadstring("http://www.google.com/")); string result = task.result;
or maybe there other solution problem.
parallel.for(0, 1000000, (i, loopstate) => { result[i] = test(i); if (result[i] == 0) { loopstate.stop(); return; } });
Comments
Post a Comment