将相同的变量传递给多个线程 [英] same variable being passed to multiple threads

查看:129
本文介绍了将相同的变量传递给多个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查此代码



hi please check this code

For icnt As Integer = 1 To 100
        Me.SetText(icnt)
        Dim gd As String = ""
        Dim gl As String = ""
        Dim cr As String = ""
        Dim lr As String = ""
        Dim tbs As String = ""
        Dim srt As String = ""
        Dim fp As String = ""
        Dim ky As String = ""
        Dim prx As String = ""

        Dim rnd As New Random
        Dim scrpurl As String = ""
        Dim bldstr As New List(Of String)

        scrpurl = GlobalVariables.domain(rnd.Next(0, GlobalVariables.googledomain.Count - 1)) & "/search?q="
        scrpurl = scrpurl & GlobalVariables.prints(rnd.Next(0, GlobalVariables.footprints.Count - 1))
        scrpurl = scrpurl & GlobalVariables.words(rnd.Next(0, GlobalVariables.keywords.Count - 1))
        scrpurl = scrpurl & GlobalVariables.gl(rnd.Next(0, GlobalVariables.googlegl.Count - 1))
        scrpurl = scrpurl & GlobalVariables.cr(rnd.Next(0, GlobalVariables.googlecr.Count - 1))
        scrpurl = scrpurl & GlobalVariables.lr(rnd.Next(0, GlobalVariables.googlelr.Count - 1))
        scrpurl = scrpurl & GlobalVariables.tbs(rnd.Next(0, GlobalVariables.googletbs.Count - 1))
        scrpurl = scrpurl & GlobalVariables.start(rnd.Next(0, GlobalVariables.googlestart.Count - 1))

        MsgBox(scrpurl)

        bldstr.Clear()
        bldstr.Add(scrpurl)

        Dim dr3 As DataRow() = dtse.Select("seurl ='" & scrpurl & "'", "se")
        If (dr3.Count = 0) Then
            Dim w1 As WaitCallback = New WaitCallback(AddressOf setdata)
            ThreadPool.QueueUserWorkItem(w1, bldstr)
        End If

    Next





可以任何人告诉我,当我显示消息框时,我看到scrpurl和setdata的不同值添加正确和100个唯一行。



当我没有显示它时setdata添加100行相同scrpurl。我知道应该做的事情,以便每个setdata得到不同的scrpurl但我在这里缺少一些东西。有人可以帮帮我吗?



问候,



Can anyone tell me when i show the messagebox i see different values for scrpurl and setdata adds correct and 100 unique rows.

When i dont show it setdata adds 100 rows of same scrpurl. I know something should be done so that each setdata gets different scrpurl but iam missing something here. Can anyone please help me out?

Regards,

推荐答案

当你展示MessageBox时,有有足够的时间执行其他线程,直到您单击消息框。当您不显示消息框时,您将拥有一个新的线程队列,这些新线程将在您的循环结束后执行。所有这些新线程都收到了你的列表bldstr,它总是包含最后一个scrpurl值。因此,所有线程都处理最后一个值。

您可以删除对bldstr.Clear()的调用,并更改它接受字符串而不是字符串列表的setdata参数,并传递bldstr(i-1)到它。
When you show the MessageBox, there is enough time for the other thread to be executed till you clicked the messagebox away. When you do not show the messagebox, you''ll have a long queue of new threads which are executed after your loop ended. And all those new threads received your list "bldstr" which always contains the last scrpurl value. Consequently, all threads work on that last value.
You could remove the call to bldstr.Clear(), and change the parameters of setdata that it accepts a string instead of a list of strings, and pass bldstr(i-1) to it.


这篇关于将相同的变量传递给多个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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