多线程并传递列表框数据 [英] multithreading and passing listbox data

查看:115
本文介绍了多线程并传递列表框数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用列表存储一些URL,并使用多线程下载每个URL.问题在于,在当前方法中,所有线程都是一次创建的.我希望能够使用25个线程,然后等待它们完成,然后再使用25个线程.现在的问题是如何检查前25个线程是否已完成?

my app uses a list to store some urls and multi threading to download each url. The problem is that in current method all threads are created at once. I want to be able to use 25 threads, then wait for them to finish and then use another 25 threads. Now the question is how do i check whether the first 25 threads are completed ?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ProgressBar1.Maximum = ListBox1.Items.Count
        For i As Integer = 0 To ListBox1.Items.Count - 1
            Dim t As New Threading.Thread(AddressOf dwnld)

            t.Start(ListBox1.Items(i).ToString)
        Next

    End Sub





Public Sub dwnld(ByVal url As String)
        Try
            Dim result As String = String.Empty

            Dim txt As String = ""

            Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)

            request.Method = "GET"

            Using stream = request.GetResponse().GetResponseStream()
                Using reader = New StreamReader(stream, Encoding.UTF8)
                    result = reader.ReadToEnd()
                End Using
            End Using

            Dim doc As New HtmlAgilityPack.HtmlDocument()

            doc.Load(New StringReader(result))

            Dim root As HtmlNode = doc.DocumentNode

            For Each link As HtmlNode In root.SelectNodes("//p")
                Dim att As String = link.InnerText.Trim
                txt = txt & att & vbCrLf
            Next

            Me.settext(txt)
        Catch ex As Exception

        End Try
    End Sub



我想做一批25个网址,等待它们完成,然后再传递下一个25个网址.当前方法一次使用所有100.



i want to do a batch of 25 urls, wait for them to finish and then pass next 25 urls. The current method uses all 100 at once. Any help please?

推荐答案

您可以像这样控制线程:

将计时器刻度设置为1秒更新.

用定义的PID启动每个线程(如果获得Windows的当前PID并选择max,则可以正确执行此操作,此时,您可以设置其他差异.
获取此PID的列表以供以后使用(仅在线程中启动的PID).
当您启动所有25个线程时,您可以启动计时器滴答.

使用此PID列表,可使用计时器刻度来控制它,如果存在PID xxx的线程不存在,则对其进行计数,直到转换等于零为止.

如果计数等于零,则可以根据需要启动另一个线程.

我希望这对您有用.

问候.
you can control the threads like this:

set a timer tick with 1 second update.

start every thread with a PID defined (you can do that correctly if you get the current PID of windows and select max, with this, you can set other differents at this moments.
get a list of this PID for use it after (only the PID you start in threads).
when you start all the 25 threads you can start the timer tick.

with this list of PID, control it with the timer tick, if the thread with the PID xxx exists or not, count it until the conter equals zero.

and if the count equals zero, you can start another threads as you want.

i hope this is useful to you.

regards.


这篇关于多线程并传递列表框数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆