Results 1 to 6 of 6

Thread: Another programming Trick :)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Another programming Trick :)

    vb.net Code:
    1. For each currentFarm in ...
    2. ...
    3.                 If urlQueue.Count > 0 Then
    4.                     Debug.Assert(currentFarm.isBusy = False)
    5.                     Debug.Assert(currentFarm.WebClient.IsBusy = False)
    6.                     ExecuteThread(urlQueue.Dequeue, currentFarm)
    7.                     'currentFarm.PackedScannable = urlQueue.Dequeue
    8.                     'currentFarm.Thread = New System.Threading.Thread(Sub() beginThreadAction.Invoke(currentFarm.PackedScannable, theFarm))
    9.                     'currentFarm.Thread.Start()
    10.                     'webClientFarm.currentFarm.getURL(urlQueue.Dequeue)
    11.                     OneOfThemBusy = True ' this time the current farm must be busy because we just told them to grab a new URL right? so at least one of them is busy
    12.                 End If
    13. ...
    14. next currentFarm
    15.  
    16.     Public Shared Sub ExecuteThread(ByVal PackedScannableParameter As String, ByVal theFarm As enchancedWinClient)
    17.         theFarm.PackedScannable = PackedScannableParameter
    18.         theFarm.Thread = New System.Threading.Thread(Sub() beginThreadAction.Invoke(PackedScannableParameter, theFarm))
    19.         theFarm.Thread.Start()
    20.     End Sub

    The commented code won't work. That's because by the time the thread start, the variable currentFarm have changed.

    So I create a new function and cache the currentFarm in the function parameter. No there will be a cache of currentFarm address (object is always a pointer) on a stack and that address is the one being passed to the System.Threading.Thread constructor.

    Tada problem solved.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Another programming Trick :)

    You've created a really complex solution to a problem that doesn't exist. All you have to do is use a variable local to the loop, e.g.
    vb.net Code:
    1. For Each item In collection
    2.     Dim currentItem = item
    3.     Dim newThread As New Thread(Sub() DoSomething(currentItem))
    4.  
    5.     newThread.Start()
    6. Next
    Also, you don't necessarily have to use a lambda expression in order to pass data to the entry method of a thread. You can use the ParameterizedThreadStart delegate instead of the ThreadStart delegate. You are limited to a single argument of type Object though, which is not ideal. If you want more information, follow the CodeBank link in my signature and check out my post on Passing Data to Threads.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Another programming Trick :)

    I am aware of ParameterizedThreadStart. It uses object which is late binding. Why use a late binding object if you can use a normal one.

    Also how do Dim currentItem = item work?

    Which variable change update closures and which variable change does not update closure? Both currentItem and item are local variables. So how come one effect the closure class and the other doesn't?

    Oh I see. currentItem "live" only on its current stack. When the code reach next currentItem disappear and a new one is created on the next loop. That's why it doesn't affect the closure.

    I forget that in vb.net scope of a local variable is not the function but the block in a function, like in C++. Each has their own stack. Am I correct here?
    Last edited by teguh123; Mar 2nd, 2011 at 03:06 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Another programming Trick :)

    Quote Originally Posted by teguh123 View Post
    I am aware of ParameterizedThreadStart. It uses object which is late binding. Why use a late binding object if you can use a normal one.
    It's not late-binding if you then cast the parameter as the appropriate type inside the method.
    Quote Originally Posted by teguh123 View Post
    Also how do Dim currentItem = item work?
    It's called "type inference". When you declare a variable and initialise it on the same line, you don't have to specify the type of the variable if the compiler can determine it from the initialising expression. For instance, in VB 2005, this code:
    vb.net Code:
    1. Dim str = "Hello World"
    will result in the 'str' variable being type Object. When no type is specified then Object is assumed. Such code is not allowed with Option Strict On, as every variable must have a type specified.

    In VB 2008 and later the rules have changed a little. All the same rules regarding Option Strict still apply. When it's On, the type of variables must be specified. The thing is, with Option Infer On, which it is by default, the type of a local variable can be specified explicitly or it can be inferred from an unambiguous initialising expression. In the code above, the initialising expression is unambiguously type String, therefore the 'str' variable's type is inferred to be String.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Another programming Trick :)

    I am aware of infer on option. You are the one that explained that to me so well. Looks like we got a school in this forum.

    I was wondering how
    #
    Dim currentItem = item suddenly solve my problem. Is my stack in block theory correct?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Another programming Trick :)

    Ah, OK. Yes, that is basically correct. Rather than the one variable whose value changes each iteration of the loop, you are basically creating a new variable each iteration.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width