Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
 
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 8-Jan-13 19:22pm
Comments
Sergey Alexandrovich Kryukov at 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 at 9-Jan-13 5:44am
   
learn how to code before finding faults. the globalvariable is a class
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 OriginalGriff 373
1 Sergey Alexandrovich Kryukov 197
2 Richard Deeming 160
3 PhilLenoir 120
4 RyanDev 115
0 Sergey Alexandrovich Kryukov 6,405
1 OriginalGriff 5,798
2 CPallini 2,473
3 Richard MacCutchan 1,647
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100