Hack Forums
[.NET] Thread for each listbox item. Need help asap! [UNSOLVED] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Thread for each listbox item. Need help asap! [UNSOLVED] (/showthread.php?tid=5076600)



Thread for each listbox item. Need help asap! [UNSOLVED] - Mr. Money™ - Nov 30, 2015

Well i want to create a new thread for each listbox item how can i do this correctly?


* - thegodhello - Nov 30, 2015

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.


* - Odus - Nov 30, 2015

(Nov 30, 2015 06:19 AM)Luxe 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.


* - Mr. Money™ - Nov 30, 2015

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!


* - Mr. Money™ - Dec 1, 2015

Still need help with this!

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


* - thegodhello - Dec 1, 2015

(Dec 1, 2015 10:06 AM)Reddington™ Wrote:  Still need help with this!

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

Replied to your private message.

🤝


* - Mr. Money™ - Dec 1, 2015

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



* - hankaf - Dec 2, 2015

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



* - Mr. Money™ - Dec 3, 2015

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



* - Jackie Chun - Dec 3, 2015

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?


* - Mr. Money™ - Dec 3, 2015

(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]


* - poopGod - Dec 3, 2015

(Dec 3, 2015 04:03 AM)Reddington™ 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



* - Odus - Dec 3, 2015

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.