Results 1 to 4 of 4

Thread: [RESOLVED] Return value from threads

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Resolved [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 =)

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

    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.
    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
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    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:
    1. Public Class MainClass
    2.     Private Threads(256) As Threading.Thread
    3.     Private PingObjects(256) As PingClass
    4.     Private Sub MySub()
    5.         For I As Integer = 0 To 255
    6.             PingObjects(I) = New PingClass
    7.             Threads(I) = New Threading.Thread(AddressOf PingObjects(I).collect)
    8.             Threads(I).IsBackground = True
    9.             Threads(I).Start()
    10.         Next
    11.     End Sub
    12. End Class
    13. Public Class PingClass
    14.     Public Sub collect()
    15. 'Ping Ips on subnet
    16. 'if IP is valid
    17. form1.listbox1.items.add(IP) 'DOES NOT WRITE even with synclock block around it
    18.  
    19.     End Sub
    20. 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...)

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Re: Return value from threads

    Ok I found a solution in case anyone else has this issue this worked quite well:

    VB Code:
    1. Public Class MainClass
    2.     Private Threads(256) As Threading.Thread
    3.     Private PingObjects(256) As PingClass
    4.     Private Sub MySub()
    5.  
    6.         For I As Integer = 0 To 255
    7.             PingObjects(I) = New PingClass
    8.             Threads(I) = New Threading.Thread(AddressOf PingObjects(I).collect)
    9.             Threads(I).IsBackground = True
    10.             Threads(I).Start()
    11.         Next
    12. for I as integer = 0 to 255
    13. threads(i).join
    14. if Threads(i).ip <> "" then
    15.    listbox1.items.add(threads(i).ip)
    16. end if
    17. next
    18.  
    19.     End Sub
    20. End Class
    21. Public Class PingClass
    22. public ip as string
    23.     Public Sub collect()
    24. 'Ping Ips on subnet
    25. 'if IP is valid
    26. ip = [VALID IP]
    27.     End Sub
    28. 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
  •  



Click Here to Expand Forum to Full Width