Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Thread for each listbox item. Need help asap! [UNSOLVED]

Well i want to create a new thread for each listbox item how can i do this correctly?
Code
Dim dictThread as new Dictionary(of String, Thread)
For i as integer = 0 to 5
   dictThread.add("Thread" & i.toString, New Thread(New ParameterizedThreadStart(AddressOf SubOrFunction))
Next

Create a dictionary, change i integer to the amount of listbox items.

Code
For Each t as Thread in dictThread.Values
   t.Start(WhateverSIs)
Next

And start them.
(Nov 30, 2015 06:19 AM)thegodhello Wrote:  Create a dictionary, change i integer to the amount of listbox items.

Or just cast them to whatever type, loop through each and pass as object to whatever method.
Hmm thanks for this i will go read this code couple of times to fully understand it :)

For more info about help i need check my other thread.
https://hackforums.net/showthread.php?tid=5077076

Still needing help though!
Still need help with this!

Check the thread link i provided in my previous reply for more info!
(Dec 1, 2015 10:06 AM)Mr. Money™ Wrote:  Still need help with this!

Check the thread link i provided in my previous reply for more info!

Replied to your private message.

🤝
I will try what you guys pmed me haha thanks for the help so far will update if it works or nah!

edit: Luxe i pmed you back regarding your pm.

Update: This is the code i use to get each list item in a seperate thread. But this code sends only the first listbox item through multiple threads what did i wrong?
Code
try
            ListBox1.Items.AddRange(mystring)

            For i As Integer = 0 To ListBox1.Items.Count()
                dictThread.Add(i.ToString, New Thread(New ParameterizedThreadStart(AddressOf doWork)))
            Next

            For Each item In ListBox1.Items

                Dim datatoSend As New ParametersToUse
                datatoSend.data = item.ToString()
            Next

            For i As Integer = 0 To ListBox1.Items.Count()
                dictThread(i).Start(datatoSend.data)
            Next

end try
You people are making things too complicated for him.
This is all you need:

Code
For i As Integer = 0 To ListBox1.Items.Count - 1
     Dim MyThread = New Thread(Sub() Me.doWork(Listbox1.Items(i).ToString))
Next
Now it starting to work but is opening 1000s of threads for first listbox item. Any idea how to fix that?

Code
For i As Integer = 0 To ListBox1.Items.Count()
                dictThread.Add(i.ToString, New Thread(Sub()
                                                          For Each item In ListBox1.Items
                                                              ' New parameters to use            
                                                              Dim datatoSend As New checkAccountParamters
                                                              ' Set parameter we want to pass to username of combolist
                                                              datatoSend.data = item.ToString()
                                                              checkAccount(datatoSend.data)
                                                          Next
                                                      End Sub))
                dictThread(i).Start()

            Next
Put For Each Item outside the dictThread, you're adding a new thread For the entire Listbox count. I think you need to define the structure in which you are using which is the Items in the listbox not the count.


Code
For Each item In ListBox1.Items
dictThread.Add(i.ToString, New Thread(Sub()
                                                              ' New parameters to use            
                                                              Dim datatoSend As New checkAccountParamters
                                                              ' Set parameter we want to pass to username of combolist
                                                              datatoSend.data = item.ToString()
                                                              checkAccount(datatoSend.data)
                                                      End Sub))
                dictThread(i).Start()
Next

shouldnt this work?
(Dec 3, 2015 02:36 AM)Jackie Chun Wrote:  Put For Each Item outside the dictThread, you're adding a new thread For the entire Listbox count. I think you need to define the structure in which you are using which is the Items in the listbox not the count.


Code
For Each item In ListBox1.Items
dictThread.Add(i.ToString, New Thread(Sub()
                                                              ' New parameters to use            
                                                              Dim datatoSend As New checkAccountParamters
                                                              ' Set parameter we want to pass to username of combolist
                                                              datatoSend.data = item.ToString()
                                                              checkAccount(datatoSend.data)
                                                      End Sub))
                dictThread(i).Start()
Next

shouldnt this work?

no, now it gives error that there is a item with the same key with its given name. Any idea how to solve it?

Code
Dim dictThread As New Dictionary(Of String, Thread)
        For i As Integer = 0 To ListBox1.Items.Count()
            For Each item In ListBox1.Items()
                dictThread.Add(i.ToString, New Thread(Sub()
                                                          ' New parameters to use            
                                                          Dim datatoSend As New checkAccountParamters
                                                          ' Set parameter we want to pass to username of combolist
                                                          datatoSend.data = item.ToString()
                                                          checkAccount(datatoSend.data)
                                                          dictThread(i).Start()
                                                      End Sub))
            Next
        Next
    End Sub

[Image: ac1af7f85902fa38d29b3862b444e576.png]
(Dec 3, 2015 04:03 AM)Mr. Money™ Wrote:  no, now it gives error that there is a item with the same key with its given name. Any idea how to solve it?

Code
Dim dictThread As New Dictionary(Of String, Thread)
        For i As Integer = 0 To ListBox1.Items.Count()
            For Each item In ListBox1.Items()
                dictThread.Add(i.ToString, New Thread(Sub()
                                                          ' New parameters to use            
                                                          Dim datatoSend As New checkAccountParamters
                                                          ' Set parameter we want to pass to username of combolist
                                                          datatoSend.data = item.ToString()
                                                          checkAccount(datatoSend.data)
                                                          dictThread(i).Start()
                                                      End Sub))
            Next
        Next
    End Sub

[Image: ac1af7f85902fa38d29b3862b444e576.png]
You want to start the thread before it is even added to the dictionary. Put the "dictThread(i).Start()" outside of the nested sub.
And why put 2 for loops in each other? You only need 1 of those...

And you dont have to explicitly call .ToString on an Integer in VB.net.

EDIT: Just do this:
Code
Dim dicTrd As New Dictionary(Of String, Thread)
        For i As Integer = 0 To ListBox1.Items.Count
            dicTrd.Add(i, New Thread(Sub()
                                         'Do stuff
                                     End Sub))
        Next
Can I ask why everyone is recommending dictionaries to monitor threads? OP said he's checking accounts. It's a short operation so ThreadPool is perfect.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by Mr. Money™Jun 29, 2017 07:41 AM 17 463 Jun 29, 2017 07:41 AM
Last Post: Mr. Money™
[C#] by matty45Apr 13, 2017 01:15 PM 9 473 Apr 13, 2017 01:15 PM
Last Post: Dan Siebels
[C#] by blondedude092Feb 7, 2016 04:48 AM 7 232 Feb 7, 2016 04:48 AM
Last Post: hackpet