Answered by:
variables of a function not colliding

Question
-
User2146470223 posted
Hi,
In a former thread about parallel functions/async tasks JBetancourt gave me this answer as a partial answer to many problems I had there:
if you extract your logic to a function and declare the variables inside the function the variables wont collide with each other, their scope will be limited to inside that function, so no need to duplicate variable declaration; think about extracting the functionality to a class instead of a function
I decided to open an own thread for this specific answer as the other thread is solved but this is still interesting.
Are there any references where I can get more information about this topic. I did not really find any on Google, so I guess I might have been using the wrong (order of) key words.
Does this also apply to/work with TPL and parallel.invoke?
Thanks in advance,
Pascal
Sunday, September 3, 2017 11:19 PM
Answers
-
User475983607 posted
The question to answer is: Will variables and arrays like suche1 or boo_spechar that are dimmed and set within function Thread1() not be replaced when my task is working on the second parallel thread1(), when this part gets to the same line where the variable is dimmed and set?Variables created inside a method are scoped to the method. Method arguments are also scoped to the method but are passed form outside the method. Therefore passing a reference type to a method and invoking the method on multiple threads can cause data collisions as well.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 4, 2017 1:37 PM
All replies
-
User-335504541 posted
Hi Translating-IT,
Do you want to know how do variables work in multithread?
If so, I think you could refer to the links below:
https://stackoverflow.com/questions/1182423/net-multi-threaded-variable-access
https://docs.microsoft.com/en-us/dotnet/standard/threading/synchronizing-data-for-multithreading
Best Regards,
Billy
Monday, September 4, 2017 9:20 AM -
User2146470223 posted
Hi Billy,
Yes, might be what I'm looking for but I dont get it for my problem:
Let's say I have:
Async Function thread1() As Task
Dim axsqlCon30 As New SqlConnection(ConStr2)
SQL30.Connection = axsqlCon30
Dim axsqlCon1 As New SqlConnection(ConStr2)
SQL1.Connection = axsqlCon1
Dim axsqlCon2 As New SqlConnection(ConStr2)
SQL2.Connection = axsqlCon2
Dim axsqlCon5 As New SqlConnection(ConStr2)
SQL5.Connection = axsqlCon5
For Each Tempit In suche100
lCounter = 0
uebja = 0
splitter2 = "pzsplit"
suche1 = Regex.Split(Tempit, splitter2)
For x = 0 To UBound(suche1) 'Wörter
If x > 0 Then
itemxx = suche1(x - 1)
End If
If suche1(x).StartsWith("pztaggi") Then
Dim match As Match = Regex.Match(suche1(x), "pztaggi\d+pztaggi")
tagsvor.Append(match)
If suche1(x).EndsWith("pztaggi") Then
Dim match2 As Match = Regex.Match(Right(suche1(x), 20), "pztaggi\d+pztaggi")
tagsnach.Insert(0, match2)
Continue For
End If
ElseIf suche1(x).StartsWith("pzmaili") Then
Dim match As Match = Regex.Match(suche1(x), "pzmaili\d+pzmaili")
tagsvor.Append(match)
If suche1(x).EndsWith("pzmaili") Then
Dim match2 As Match = Regex.Match(Right(suche1(x), 20), "pzmaili\d+pzmaili")
tagsnach.Insert(0, match2)
Continue For
End If
ElseIf suche1(x).StartsWith("pzlinki") Then
Dim match As Match = Regex.Match(suche1(x), "pzlinki[\d]*pzlinki")
Continue For
tagsvor.Append(match)
If suche1(x).EndsWith("pzlinki") Then
Dim match2 As Match = Regex.Match(Right(suche1(x), 20), "pzlinki[\d]*pzlinki")
tagsnach.Insert(0, match2)
Continue For
End If
Else
If ganzsatz.TryGetValue(Tempit, str_item10) Then
sb_proofed2.Append("pzsplit" & tagsvor.ToString & str_item10 & tagsnach.ToString)
tagsvor.Clear()
tagsnach.Clear()
Exit For
End If
End If
If int_wortcount > 0 Then
int_wortcount = int_wortcount - 1
Continue For
End If
int_ubounddiff = UBound(suche1) - x
Tempit3 = Regex.Replace(Regex.Replace(Trim(suche1(x)), "pztaggi[\d]*pztaggi", ""), "pzsplit\?[\d]*\?pzsplit", "")
If x < UBound(suche1) Then
item = Regex.Replace(Regex.Replace(Trim(suche1(x + 1)), "pztaggi[\d]*pztaggi", ""), "pzsplit\?[\d]*\?pzsplit", "")
End If
'Response.Write(Tempit3 & "-")
Tempit3b = Tempit3
If Tempit3 = Tempit3.ToUpper() Then
boo_wortup = 1
Else
boo_wortup = 0
End If
Dim boo_spechar As Boolean
' ############## and many more lines in between #################'
Next
If ganzsatz.TryGetValue(Tempit, str_item10) Then
Else
ganzsatz.TryAdd(Tempit, satzneu.ToString)
End If
satzneu.Clear()
Next 'Sätze Ende
End Functionand I want to run this task (thread1) on 2 threads with:
Parallel.Invoke(Function() Try thread1() Catch ex As Exception anz.Append("<strong>" & Resources.text.error & "</strong>") End Try End Function, Function() Try thread1() Catch ex As Exception anz.Append("<strong>" & Resources.text.error & "</strong>") End Try End Function)
whereas both get fed with different starting values (I know the way it is above does not work out like this right now, so no need to correct it for me).
The question to answer is: Will variables and arrays like suche1 or boo_spechar that are dimmed and set within function Thread1() not be replaced when my task is working on the second parallel thread1(), when this part gets to the same line where the variable is dimmed and set?regards,
Pascal
Monday, September 4, 2017 11:37 AM -
User475983607 posted
The question to answer is: Will variables and arrays like suche1 or boo_spechar that are dimmed and set within function Thread1() not be replaced when my task is working on the second parallel thread1(), when this part gets to the same line where the variable is dimmed and set?Variables created inside a method are scoped to the method. Method arguments are also scoped to the method but are passed form outside the method. Therefore passing a reference type to a method and invoking the method on multiple threads can cause data collisions as well.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 4, 2017 1:37 PM -
User2146470223 posted
Hi mgebhard,
That was also what I found that far but I was not sure whether I might have missed something.
I guess in the last thread JBetancourt did not get that I used the very same function/method as parallel thread. So when I explained that I had cloned the function to Thread2() while changing all the variables to var1 -> var100, var2 -> var200 he said there is no need to do so as vars would not be colliding anyway.
Maybe he thought I was using some other multithreading like parallel.foreach. From what I understood, there the inbuilt logic prevents such collision.
Monday, September 4, 2017 2:51 PM