Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Threading and inserting data from one thread to another

Status
Not open for further replies.

Antithott

IS-IT--Management
Jan 20, 2006
90
DK
Hi,

Ive been trying to learn how to pass data from one thread to another, i read the guides here on Tek-tips and used google, but i cant seem to understand it.

I can make a simple thread, but i cant get it to use the data in my UI.

I have a form with a listview and 2 buttons, the listview is set to details, and have 3 columns, I want the Backgroundprocess Thread to insert data into the listview.

Code :
Code:
Imports System.threading

Public Class Form1

    Dim t As Thread


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        t = New Thread(AddressOf Me.BackgroundProcess)
        t.Start()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       

    End Sub

    Private Sub BackgroundProcess()
        Dim i As Int32 = 1

        Do Until i = 10
            Dim item As ListViewItem = ListView1.Items.Add("Iterations: " & i)
            item.SubItems.Add("Iterations: " & i)
            item.SubItems.Add("Iterations: " & i)
            i += 1
        Loop
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        t.abort()
    End Sub
End Class

Im hoping someone here could help me along the way and perhaps show me the code i need to add for this to work.
 
Well finally found the solution myself, dont know if its the correct way to do it, but it work and does the job.


Code:
Imports System.threading

Public Class Form1

    Dim NewThread As Thread


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        NewThread = New Thread(AddressOf Me.DoWork)
        NewThread.Start()
    End Sub

    Private Sub BackgroundProcess()
        Dim i As Int32 = 1

        Do Until i = 10
            Dim item As ListViewItem = ListView1.Items.Add("Iterations: " & i)
            item.SubItems.Add("Iterations: " & i)
            item.SubItems.Add("Iterations: " & i)
            i += 1
        Loop

    End Sub


    Private Sub DoWork()
        Thread.Sleep(10000)
        Dim i As Int32 = 1

        Do Until i = 10
            Dim Tekst As String = "test" & i

            Me.Invoke(New UpdateProgressDelegate(AddressOf UpdateProgress), Tekst, "Subitem1-", "Subitem2-")
           i += 1
        Loop
    End Sub

    Private Delegate Sub UpdateProgressDelegate(ByVal LvItemText As String, ByVal LvSubItemText As String, ByVal LvSubItemText1 As String)

    Private Sub UpdateProgress(ByVal LvItemText As String, ByVal LvSubItemText As String, ByVal LvSubItemText1 As String)
        Dim item As ListViewItem = ListView1.Items.Add(LvItemText)
        item.SubItems.Add(LvSubItemText)
        item.SubItems.Add(LvSubItemText1)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        NewThread.Abort()
    End Sub
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top