Click here to Skip to main content
11,802,762 members (72,200 online)
Rate this: bad
Please Sign up or sign in to vote.
hi please check this code

For icnt As Integer = 1 To 100
        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.googlegl.Count - 1))
        scrpurl = scrpurl &, GlobalVariables.googlecr.Count - 1))
        scrpurl = scrpurl &, 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))
        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

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?

Posted 8-Jan-13 19:22pm
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!)?
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...


1 solution

Rate this: bad
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.
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?

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

  Print Answers RSS
0 KrunalRohit 366
1 OriginalGriff 365
2 F-ES Sitecore 295
3 Afzaal Ahmad Zeeshan 230
4 CPallini 230
0 OriginalGriff 2,950
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,695
4 Richard MacCutchan 1,157

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2015
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