vb.net - Background worker not working right -


i have created background worker go , run pretty long task includes creating more threads read file of urls , crawl each. tried following through debugging , found background process ends prematurely no apparent reason. there wrong in logic of code causing this. try , paste as possible make sense.

            while not myreader.endofdata                         try                             currentrow = myreader.readfields()                             dim currentfield string                             each currentfield in currentrow                                 itemcount = itemcount + 1                                 searchitem = currentfield                                 generatesearchfromfile(currentfield)                                 processquerysearch()                             next                         catch ex microsoft.visualbasic.fileio.malformedlineexception                             console.writeline(ex.message.tostring)                         end try                      end while 

this first bit of code loop input file , background worker does. next bit of code background worker creates threads work 'landingpages'. after 10 threads created background worker exits sub , skips file input loop , exits program.

     try                 each landingpage string in landingpages                     pgbar.timer1.stop()                     if visitedpages.contains(landingpage)                         continue                     else                         dim thread = new thread(addressof processquery)                         count = count + 1                         thread.name = "worm" & count                         thread.start(landingpage)                         if numthread >= 10                             each thread in threadlist                                 thread.join()                             next                              numthread = 0                             continue                         else                             numthread = numthread + 1                             synclock threadlist                                 threadlist.add(thread)                             end synclock                         end if                     end if                    next 

my main program create background thread follows:

private sub btnsearch_click(byval sender system.object, byval e    system.eventargs) handles btnsearch.click      isclicked = true     progressbar1.value = 10     me.backgroundworker1.runworkerasync()     timer1.interval = 10000      .... 

background work function not shown calls function in class....

blogdiscoverobj.start() 

now tried wait threads here in second block of code above: dim thread = new thread(addressof processquery) count = count + 1 thread.name = "worm" & count thread.start(landingpage) if numthread >= 10 each thread in threadlist thread.join() next numthread = 0 continue else numthread = numthread + 1 synclock threadlist threadlist.add(thread) end synclock end if end if next

           thread.sleep(1000)            each thread in threadlist                 thread.join()             next 

hope clearer

also main thread runs forms background called run main thread supposed wait background process end unless user selects option main form.

how main program ? starting background threads. background thread stopped, whenever no more foreground threads program exists.

you need wait threads started finish before exiting main().


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -