|
-
Mar 2nd, 2011, 12:46 AM
#1
Thread Starter
Fanatic Member
Another programming Trick :)
vb.net Code:
For each currentFarm in ...
...
If urlQueue.Count > 0 Then
Debug.Assert(currentFarm.isBusy = False)
Debug.Assert(currentFarm.WebClient.IsBusy = False)
ExecuteThread(urlQueue.Dequeue, currentFarm)
'currentFarm.PackedScannable = urlQueue.Dequeue
'currentFarm.Thread = New System.Threading.Thread(Sub() beginThreadAction.Invoke(currentFarm.PackedScannable, theFarm))
'currentFarm.Thread.Start()
'webClientFarm.currentFarm.getURL(urlQueue.Dequeue)
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
End If
...
next currentFarm
Public Shared Sub ExecuteThread(ByVal PackedScannableParameter As String, ByVal theFarm As enchancedWinClient)
theFarm.PackedScannable = PackedScannableParameter
theFarm.Thread = New System.Threading.Thread(Sub() beginThreadAction.Invoke(PackedScannableParameter, theFarm))
theFarm.Thread.Start()
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.
-
Mar 2nd, 2011, 01:05 AM
#2
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:
For Each item In collection Dim currentItem = item Dim newThread As New Thread(Sub() DoSomething(currentItem)) newThread.Start() 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.
-
Mar 2nd, 2011, 03:03 AM
#3
Thread Starter
Fanatic Member
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.
-
Mar 2nd, 2011, 03:26 AM
#4
Re: Another programming Trick :)
 Originally Posted by teguh123
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.
 Originally Posted by teguh123
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: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.
-
Mar 2nd, 2011, 03:32 AM
#5
Thread Starter
Fanatic Member
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?
-
Mar 2nd, 2011, 03:53 AM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|