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

.NET 

cross-thread, cross-class adding items to a listbox. [RESOLVED WITH CODE]

UPDATE: Thanks everybody who replied and tried to help me, I learned some new things today :).

Here is how I solved the problem.
NOTE: all this code is done in the main form containing the listbox.

Thanks to Odus btw :P

Firstly in your other class containing the thread put
Code
Public Shared flag as Object

Secondly the function to invoke the listbox:
Code
Public Delegate Sub DelUpdateListBox(lb As ListBox, item As String)
    Public Sub UpdateListBox(lb As ListBox, item As String)
      If Me.InvokeRequired Then
      Me.Invoke(New DelUpdateListBox(AddressOf UpdateListBox), New Object() {lb, item})
      Else
      lb.Items.Add(item)
      End If
    End Sub

^^ Does not have to be public I just did that because I'm a skid and thought I needed it....

Alright great! Now we need some variables..
Code
' // We are going to be doing this on another sub, so to stop it from writing the item multiple times we need this.
Private written As Boolean = False
' comparing the lastitem with the current to make sure there not the same.
Private lastline As String

Awesome. So now create a new sub named "additemtolistbox" (or whatever you prefer)
In this sub put this:
Code
If yourotherclass.flag Is Nothing Then

      Return

      End If

      If Not yourotherclass.flag Is Nothing And Not yourotherclass.flag Is lastline Then
      lastline = CStr(yourotherclass.flag)
      UpdateListBox(console, CStr(yourotherclass.flag))

      End If
NOTE: Option Strict and Explicit is on.

Alright 2 more things now.
We basically want this sub to loop, so the lazy way to do so is call the sub in form paint event.
So now in your other thread in the other class, simply put
Code
flag = "item to add"

That should give you a basic idea of how this would work. This code is probably buggy, but I hope it helps some of you :)
Saw thread title and walked in, then I saw the code.

[Image: 0WHGKhB.gif]
(Jul 16, 2015 12:01 AM)tweety Wrote:  Saw thread title and walked in, then I saw the code.

ahhaha yea I'm sort of a vb noob.
(Jul 15, 2015 11:38 PM)Vrekt Wrote:  
Code
Control.CheckForIllegalCrossThreadCalls = False
NO
NO
NONONO
NONONONONONO
NONONO
NO

SERIOUSLY JUST FUCKING NO
AND IF YOU THINK ABOUT IT AGAIN THEN NO. That is my input for this.
Bitcoin Address: 1KQCBvevk1sA4wSj7gugv3sf5yjUygAzcv
this is so new lmao idk what this is


I need help learning this would make more sense
Off the top of my head, in the form that has the listbox, create the following function:
Code
Public Function addToListBox(ByVal LB As ListBox, ByVal item As Object) As Boolean
      Try
      If LB.InvokeRequired Then
            LB.Invoke(New MethodInvoker(Sub()
            LB.Items.Add(item)
            End Sub))
      Else
            LB.Items.Add(item)
      End If
      Return True
      Catch ex As Exception
      MsgBox("Error when adding item:" + vbCrLF + vbCrLf + ex.ToString(), vbExclamation + vbOkOnly, "Error")
      End Try
      Return False
    End Function
(Jul 15, 2015 11:38 PM)Vrekt Wrote:  
Code
' // ignore this not really relevant

      ' // Check if notepad is running, if so decide what to do based on config.

      ' // Basic variable to tell the thread if the application is open, used for my loop here.

      ' // find the process

      ' // if its open

      ' // if a person wanted to close it when it started up execute this code

      ' // if they wanted to warn the user via a listbox on the main form.

      ' // wait 1ms before continuing
For reference:
VB comment: '
C# comment: //
There's no need to do both last time I checked.
(Jul 16, 2015 01:33 AM)IntelliJ Wrote:  For reference:
VB comment: '
C# comment: //
There's no need to do both last time I checked.

User is asshat- But not wrong
(Jul 16, 2015 01:34 AM)HFTetramorph Wrote:  User is asshat- But not wrong

Thanks sweetheart. 🤗
(Jul 16, 2015 01:33 AM)IntelliJ Wrote:  For reference:
VB comment: '
C# comment: //
There's no need to do both last time I checked.

you are correct, just I prefer the look.
Quote:Control.CheckForIllegalCrossThreadCalls = False


NO NO NO! JUST PROPERLY INVOKE THE NEXT THREAD! This is terrible practice and should never be used.
(Jul 16, 2015 01:23 AM)Josh‎ Wrote:  Off the top of my head, in the form that has the listbox, create the following function:
Code
Public Function addToListBox(ByVal LB As ListBox, ByVal item As Object) As Boolean
      Try
      If LB.InvokeRequired Then
      LB.Invoke(New MethodInvoker(Sub()
      LB.Items.Add(item)
      End Sub))
      Else
      LB.Items.Add(item)
      End If
      Return True
      Catch ex As Exception
      MsgBox("Error when adding item:" + vbCrLF + vbCrLf + ex.ToString(), vbExclamation + vbOkOnly, "Error")
      End Try
      Return False
    End Function

Nah didn't work, I must be doing something wrong, I have tried something similar to this before. I'm using this code to use the function.
NOTE: para_main is the form with the listbox.
Code
para_main.addToListBox(para_main.console, "test")
Thanks for the suggestion tho 🙂
Delegate!

Code
Private Delegate Sub DelUpdateListBox(lb As ListBox, item As String)
Private Sub UpdateListBox(lb As ListBox, item As String)
    If Me.InvokeRequired Then
      Me.Invoke(New DelUpdateListBox(AddressOf UpdateListBox), New Object() {lb, item})
    Else
      lb.Items.Add(item)
    End If
End Sub

Still not the most efficient way to do it though, I recommend the ManagementEventWatcher class.
(Jul 16, 2015 02:17 AM)Odus Wrote:  Delegate!

Code
Private Delegate Sub DelUpdateListBox(lb As ListBox, item As String)
Private Sub UpdateListBox(lb As ListBox, item As String)
    If Me.InvokeRequired Then
      Me.Invoke(New DelUpdateListBox(AddressOf UpdateListBox), New Object() {lb, item})
    Else
      lb.Items.Add(item)
    End If
End Sub

Thanks so much I implemented your code and it worked! Thanks everybody else as well.. I learned some new things today :). I updated my post with how I solved the problem, (very inefficient, but works) for anybody else that has the problem.
(Jul 16, 2015 02:54 AM)Vrekt Wrote:  Thanks so much I implemented your code and it worked! Thanks everybody else as well.. I learned some new things today :). I updated my post with how I solved the problem, (very inefficient, but works) for anybody else that has the problem.

Did you remove the CheckForIllegalCrossThreads = False? If not please do.
Bitcoin Address: 1KQCBvevk1sA4wSj7gugv3sf5yjUygAzcv


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by nimrod313Aug 15, 2021 10:59 AM 1 84 Aug 15, 2021 10:59 AM
Last Post: Mohammedkh21
[C] by lockheadApr 22, 2021 07:10 AM 0 147 Apr 22, 2021 07:10 AM
Last Post: lockhead
[.NET] by GhostenigmaJun 12, 2019 05:52 AM 5 226 Jun 12, 2019 05:52 AM
Last Post: HFDone