|
-
Aug 2nd, 2006, 05:33 PM
#1
Thread Starter
Junior Member
[RESOLVED] Return value from threads
I have an application that will ping all IP's on a subnet each on a seperate thread. This works great, except I can not get results from all of those threads. I would like any thread that gets a vaid ping responce to save the IP it pinged in an array, but if I have The threads try and write to an array they all attempt to acces about at the same time and no data gets written. What is the best way to obtain a list of output for each thread in a thread array when they are all running the same procedure at the same time? If i am not clear enough let me know and I will post some code =)
-
Aug 2nd, 2006, 08:03 PM
#2
Re: Return value from threads
I strongly suggest that you follow the Articles -> Advanced .NET link in my signature and read the Managed Threading section. Synchronising threads is one of the most fundamental tasks when using multiple threads.
-
Aug 3rd, 2006, 10:17 AM
#3
Thread Starter
Junior Member
Re: Return value from threads
Thats a lot of good info, but where it ultimately got me is use the synclock function, which i tried with no luck at all.
VB Code:
Public Class MainClass
Private Threads(256) As Threading.Thread
Private PingObjects(256) As PingClass
Private Sub MySub()
For I As Integer = 0 To 255
PingObjects(I) = New PingClass
Threads(I) = New Threading.Thread(AddressOf PingObjects(I).collect)
Threads(I).IsBackground = True
Threads(I).Start()
Next
End Sub
End Class
Public Class PingClass
Public Sub collect()
'Ping Ips on subnet
'if IP is valid
form1.listbox1.items.add(IP) 'DOES NOT WRITE even with synclock block around it
End Sub
End Class
How can I get sync lock to work appropriately for this (I have looked over its articles and tried to duplicate their examples several ways with no luck...)
-
Aug 3rd, 2006, 03:26 PM
#4
Thread Starter
Junior Member
Re: Return value from threads
Ok I found a solution in case anyone else has this issue this worked quite well:
VB Code:
Public Class MainClass
Private Threads(256) As Threading.Thread
Private PingObjects(256) As PingClass
Private Sub MySub()
For I As Integer = 0 To 255
PingObjects(I) = New PingClass
Threads(I) = New Threading.Thread(AddressOf PingObjects(I).collect)
Threads(I).IsBackground = True
Threads(I).Start()
Next
for I as integer = 0 to 255
threads(i).join
if Threads(i).ip <> "" then
listbox1.items.add(threads(i).ip)
end if
next
End Sub
End Class
Public Class PingClass
public ip as string
Public Sub collect()
'Ping Ips on subnet
'if IP is valid
ip = [VALID IP]
End Sub
End Class
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
|