Click here to Skip to main content
15,860,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi please check this code

VB
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


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,
Posted
Comments
Sergey Alexandrovich Kryukov 9-Jan-13 1:53am    
The question is not clear. What variables? How did you try to synchronize access to them by different threads?
By the way, do you understand that there is no such thing as "global" in .NET (thanks goodness! finally!)?
—SA
amit_upadhyay 9-Jan-13 5:44am    
learn how to code before finding faults. the globalvariable is a class
Sergey Alexandrovich Kryukov 9-Jan-13 9:06am    
I understand... this is just the naming. There is a name: "singleton", but better read about it in Wikipedia. There is no such concept as "global", but singleton is close to it (and rarely needed).
And using "variable" or "class" in the name of class is silly, sorry. All then names should be semantic.

You don't show your threads, so how can you expect some useful advice? Who knows what you are doing and what did you want to do? Anyway, to me it looks like you don't quite understand yourself. Maybe you do, but nobody can see it...

—SA

1 solution

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Jan-13 9:11am    
Even if you are right in fact, I cannot understand where do you see all that? First of all, where do you see any threads? message box?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900