# .NET VB indexoutofrangeexception was unhandled



## loserOlimbs (Jun 19, 2004)

I am getting an error message for this code when I try to run it, can anyone give me somepointers on whats wrong here?

The program is supposed to multithread based on available cores, and max them out.
The liune the error (in the title) points to is:
Thread(StartsWork) = New Thread(AddressOf ProcessTester)

Code below line
_______________________________________________________________________________________

Option Strict On
Imports System.Reflection
Imports System.Threading


Public Class frmMain
Private Thread(intCores) As Thread
 Private intCores As Integer
Private progress As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call ProcessTester()
End Sub

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
intCores = System.Environment.ProcessorCount
lblCores.Text = "Number of Processors: " & Convert.ToString(intCores)
End Sub

Private Sub ProcessTester()
Dim intStopWork As Integer = 100000
Dim dblWorker As Double
Dim i As Integer
Dim StartsWork As Integer = 0

prgCalculations.Maximum = intStopWork + 1

For StartsWork = 0 To intCores Step 1
Thread(StartsWork) = New Thread(AddressOf ProcessTester)
Thread(StartsWork).IsBackground = True
Thread(StartsWork).Start()
For i = 1 To intStopWork Step 1
dblWorker = Math.Tan(Math.PI / 180)

Next i
Next StartsWork

End Sub
End Class


----------



## Ent (Apr 11, 2009)

I'd guess that ProcessorCount is base 1, while the array is base 0. That is on a single processor machine the processor is (0) while on a 2 processor machine they are (0) and (1). The for loop should be For StartsWork = 0 To intCores - 1 Step 1


----------



## loserOlimbs (Jun 19, 2004)

Hey, looks like a great suggestion. Thanks.

Will let you know if it works.


----------



## loserOlimbs (Jun 19, 2004)

No good, still getting the error...


----------



## Ent (Apr 11, 2009)

Where does "AddressOf ProcessTester" come from?


----------



## loserOlimbs (Jun 19, 2004)

that's the function that the call resides in.

Private Sub ProcessTester()
all my code for the function


----------



## integritysercom (Jun 3, 2010)

For StartsWork = 0 To intCores Step 1

make it

For StartsWork = 0 To (intCores -1) Step 1


----------



## loserOlimbs (Jun 19, 2004)

Did not work, I believe this might have something to do with the address space... not entirely sure.

If someone wants a copy of the solution I can post a link for download.


----------

