CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2010
    Posts
    23

    [RESOLVED] Add items to another form's listbox from a thread

    Completed:

    'Form 1- start a new thread and declare a new second form. Have it invoke before adding an entry.
    'Please let me know if there are more efficient ways of doing this- as this apart from "CheckForIllegalCrossThreadCalls = false" is a workaround.
    Code:
    Public Class Form1
        Dim t As New Thread(AddressOf startanew)
    
        Private Sub startanew()
            While t.IsAlive
                While form21.Timer1.Enabled = True
                    Thread.Sleep(500)
                    add("item")
                End While
            End While
        End Sub
        Dim form21 As New Form2
        Private Delegate Sub worklistbox(ByVal item As Object)
    
        Private Sub add(ByVal item As Object)
            If form21.ListBox1.InvokeRequired Then
                form21.ListBox1.Invoke(New worklistbox(AddressOf Me.add), item)
                'bypass error and invoke
            Else
                ' This is the UI thread so perform the task.
                form21.ListBox1.Items.Add(item)
            End If
        End Sub
        Private Sub closer(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosing
    t.abort
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            form21.Timer1.Enabled = True
            form21.Show()
            t.Start()
        End Sub
    'Form 2 toggles the timer2, which pauses/resumes the work of the thread.
    Code:
    Public Class Form2
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Timer1.Enabled = True
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Timer1.Enabled = False
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If ListBox2.Items.Count > 20 Then
                ListBox2.Items.Clear()
            Else
                ListBox2.Items.Add("Timer2 is Running")
            End If
        End Sub
    End Class
    Last edited by StriderH2; April 6th, 2011 at 07:33 AM.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Add items to another form's listbox from a thread

    Hello Strider - did you edit your initial question?? If so, please do not do that, as people will not know what your issue was. This response ( mine ) is based on your earlier question, so now, it basically defeats the whole purpose, and it took me a while too, so, technically all my effort was for nothing, as the original question is lost. Can just as well delete this thread, because people searching for this answer to this Popular and difficult question won't know that this is an indeed an answer to their question..........

    You cannot call UI methods and properties directly from another thread than the one the form was created on, so you'll need to use Invoke to marshall the call to the control's thread.


    Something like :

    Code:
    Imports System.Threading
    
    Public Class Form1
        Dim Form2 As New Form2
        Dim strTest As String = "test"
    
        'start another thread to show how to add controls from another thread
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim t1 As New System.Threading.Thread(AddressOf thread1)
    
            t1.Start()
            Form2.Show()
            Form2.TopMost = True
    
        End Sub
    
        Dim ListBox1 As New ListBox
    
        Private Sub thread1()
            ListBox1.Location = New Point(50, 50)
            ListBox1.Width = 300
            ListBox1.Height = 250
            ListBox1.Items.Add(strTest)
    
    
            'call a special sub to add the control as 
            'we are not allowed to add controls directly
            AddControlToForm(ListBox1)
    
        End Sub
    
        ' prototype for the delegate 
        Private Delegate Sub AddControlToFormDelegate(ByVal ctrl As Control)
    
        Private Sub AddControlToForm(ByVal ctrl As Control)
            'Add string control ctrl to form me
    
            'InvokeRequired lets us know if we are in the wrong thread to
            'access form Me
            If Me.InvokeRequired Then
                ' make a delegate that will fire this sub again
                Dim delegate1 As New AddControlToFormDelegate(AddressOf _
            AddControlToForm)
                ' hold the parameters - the control being sent
                Dim parameters(0) As Object
                parameters(0) = ctrl
                'Ask form me to call this sub using the delegate with the parameters.
                Me.Invoke(delegate1, parameters)
                'it will call from the correct thread
            Else
                'InvokeRequired lets us know we are in the correct thread
                'so we can use listview1.add safely
                Form2.Controls.Add(ctrl)
            End If
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            thread1()
        End Sub
    End Class
    I am attaching the sample with


    It may not be perfect, but it works
    Attached Files Attached Files
    Last edited by HanneSThEGreaT; April 6th, 2011 at 07:33 AM.

  3. #3
    Join Date
    Oct 2010
    Posts
    23

    Re: [RESOLVED] Add items to another form's listbox from a thread

    I marked the thread as resolved and edited it with the new solution before you posted- but will definitely look at the code you provided. Thank you!

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

Featured