vb.net - How to pause loop while multithreading is alive -


i have 3 threads called inside loop.

for integer = 0 dg.rows.count - 1     dim thread1 = new system.threading.thread(addressof processdata)     dim thread2 = new system.threading.thread(addressof processdata2)     dim thread3 = new system.threading.thread(addressof processdata3)     if not thread1.isalive        x1 =        thread1.start()     elseif not thread2.isalive        x2 =        thread2.start()     elseif not thread3.isalive         x3 =         thread3.start()     end if next 

how pause loop while threads alive? want is, if 1 of threads finishes continue loop , (i), pause loop again if there no available threads. because dg.rows items more 3.

let framework handle you: use threadpool.

first, create array hold thread status each item:

dim doneevents(dg.rows.count) manualresetevent 

like x1,x2,x3 variables, needs accessible both main thread , processdata method.

then modify processdata method accept object argument @ beginning , set resetevent @ end:

public sub processdata(byval data object)    dim x integer = cint(data)      '...     'existing code here      doneevents(x).set() end sub 

now can queue them this:

for integer = 0 dg.rows.count - 1     threadpool.queueuserworkitem(processdata, i) next waithandle.waitall(doneevents) console.writeline("all data processed.") 

though suspect should pass data grid each row processdata method.

you can use newer async/await keywords, i'll have hard time writing sample without knowing of contents of processdata.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -