Hack Forums
[.NET] cross-thread, cross-class adding items to a listbox. [RESOLVED WITH CODE] - 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] cross-thread, cross-class adding items to a listbox. [RESOLVED WITH CODE] (/showthread.php?tid=4908636)

Pages: Pages 1 2


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

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 :)


* - tweety - Jul 16, 2015

Saw thread title and walked in, then I saw the code.

[Image: 0WHGKhB.gif]


* - Vrekt - Jul 16, 2015

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


* - Vysse - Jul 16, 2015

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


* - Shyon143 - Jul 16, 2015

this is so new lmao idk what this is


I need help learning this would make more sense


* - Josh‎ - Jul 16, 2015

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



* - IntelliJ - Jul 16, 2015

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


* - HFTetramorph - Jul 16, 2015

(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


* - IntelliJ - Jul 16, 2015

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

Thanks sweetheart. 🤗


* - Vrekt - Jul 16, 2015

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


* - HFTetramorph - Jul 16, 2015

Quote:Control.CheckForIllegalCrossThreadCalls = False


NO NO NO! JUST PROPERLY INVOKE THE NEXT THREAD! This is terrible practice and should never be used.


* - Vrekt - Jul 16, 2015

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


* - Odus - Jul 16, 2015

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.


* - Vrekt - Jul 16, 2015

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


* - Vysse - Jul 16, 2015

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