vb.net - Multithreading not outputting all -
i'm not sure i'm doing wrong. have simple programme using thread pool execute 9 tasks. each task starts 3 new threads retrieve data. calling thread processes results. should execute varying times output results immediate window. not tasks output anything. 7 8 results. cannot predict results output; random. why not results output?
class work3 sub mainloop() dim callback new waitcallback(addressof doonecustomer) loopct integer = 1 9 'data in dim datain new datain("cust" & loopct, loopct) 'run code on thread pool threadpool.queueuserworkitem(callback, datain) next end sub function doonecustomer(byval datain2 object) boolean dim datain datain = ctype(datain2, datain) dim tub new datacontainer(datain.custid, datain.int) 'start new info threads 'get info in parallel dim t1 new thread(addressof getdata1) dim t2 new thread(addressof getdata2) dim t3 new thread(addressof getdata3) t1.start(tub) t2.start(tub) t3.start(tub) 'join 3 threads ensure finished getting info t1.join() t2.join() t3.join() if makedecision(tub) 'process results in-line; no new thread doprocessdata(tub) end if homecoming true end function function makedecision(byref tub datacontainer) boolean homecoming true end function sub doprocessdata(byval mytub datacontainer) debug.print(mytub.data1.qbname & _ " end bal: " & mytub.data1.endbal.tostring("#,##0") & _ " inv1 " & mytub.data2.total.tostring("#,##0") & _ " inv2 " & mytub.data3.total.tostring("#,##0")) end sub sub getdata1(byval mytub2 object) dim mytub datacontainer = ctype(mytub2, datacontainer) dim d new detaildata d.qbname = mytub.custid d.beginbal = mytub.int * 10000 + mytub.int d.endbal = mytub.int * 100000 + mytub.int dim num integer = calculate(mytub.int) 'put info mytub.data1 = d end sub sub getdata2(byval mytub2 object) dim mytub datacontainer = ctype(mytub2, datacontainer) dim inv new invoicedata inv.qbcustname = mytub.custid inv.txndate = dateadd(dateinterval.day, new random(1).next(1, 30), now) inv.total = mytub.int * 1000 + mytub.int dim num integer = calculate(mytub.int) 'put info mytub.data2 = inv end sub sub getdata3(byval mytub2 object) dim mytub datacontainer = ctype(mytub2, datacontainer) dim inv new invoicedata inv.qbcustname = mytub.custid inv.txndate = dateadd(dateinterval.day, new random(1).next(1, 30), now) inv.total = mytub.int * 100 dim num integer = calculate(mytub.int) 'put info mytub.data3 = inv end sub end class class datain public int integer public custid string public sub new(byval _custid string, byval _int integer) int = _int custid = _custid end sub public sub new() end sub end class class datacontainer public int integer public custid string public data1 detaildata public data2 invoicedata public data3 invoicedata public sub new(byval _custid string, byval _int integer) int = _int custid = _custid end sub public sub new() end sub end class class detaildata public qbname string public beginbal decimal public endbal decimal end class class invoicedata public qbcustname string public txndate date public total decimal end class 'calculate fibonacci increment calculation time varying amounts public function calculate(byval n integer) integer if n <= 1 homecoming n end if homecoming calculate(n - 1) + calculate(n - 2) end function
output looks this
cust1 end bal: 100,001 inv1 1,001 inv2 100 cust4 end bal: 400,004 inv1 4,004 inv2 400 cust2 end bal: 200,002 inv1 2,002 inv2 200 cust7 end bal: 700,007 inv1 7,007 inv2 700 cust6 end bal: 600,006 inv1 6,006 inv2 600 cust5 end bal: 500,005 inv1 5,005 inv2 500 cust8 end bal: 800,008 inv1 8,008 inv2 800 cust9 end bal: 900,009 inv1 9,009 inv2 900
.net fiddle
i think problem debug.print. think using debug.print assess code flawed idea. why should think debug window can handle multiple simultaneous input.
when using msgbox output, code performs expected. info output. multiple msgbox windows not problem vb.net.
i tried putting thread.sleep in code. works fine too. presumably because output no longer hitting debug window simultaneously.
so in end problem not code per se output device. code needs rewritten business relationship limitations of output device.
vb.net multithreading
No comments:
Post a Comment