Click here to Skip to main content
15,918,243 members
Home / Discussions / Visual Basic
   

Visual Basic

 
Questiongenerating more than one dataset based upon parameter passed Pin
uglyeyes16-Mar-06 16:42
uglyeyes16-Mar-06 16:42 
QuestionIssue updating Listview via Thread Pin
mechman16-Mar-06 13:03
mechman16-Mar-06 13:03 
AnswerRe: Issue updating Listview via Thread Pin
Dave Kreskowiak17-Mar-06 1:47
mveDave Kreskowiak17-Mar-06 1:47 
GeneralRe: Issue updating Listview via Thread Pin
mechman17-Mar-06 3:14
mechman17-Mar-06 3:14 
GeneralRe: Issue updating Listview via Thread Pin
mechman17-Mar-06 3:17
mechman17-Mar-06 3:17 
GeneralRe: Issue updating Listview via Thread Pin
Dave Kreskowiak17-Mar-06 7:25
mveDave Kreskowiak17-Mar-06 7:25 
GeneralRe: Issue updating Listview via Thread Pin
mechman17-Mar-06 7:53
mechman17-Mar-06 7:53 
GeneralRe: Issue updating Listview via Thread Pin
Dave Kreskowiak17-Mar-06 12:37
mveDave Kreskowiak17-Mar-06 12:37 
OK. How about this...
This code assumes you have a form with a Button and a ListView on it...
    Private Delegate Sub AddItemToListViewDelegate(ByVal item As String)
    Private Delegate Sub ChangeSubItemDelegate(ByVal index As Integer, ByVal item As String)
 
    Private t1 As Thread
    Private t2 As Thread
 
    Private Const MAXITEMS As Long = 20
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'AddItems()
        ListView1.Items.Clear()
 
        t1 = New Thread(New ThreadStart(AddressOf AddItems))
        t1.Start()
        Thread.Sleep(100)
        t2 = New Thread(New ThreadStart(AddressOf RandomSubItems))
        t2.Start()
    End Sub
 
    Private Sub AddItemToListView(ByVal item As String)
        If ListView1.InvokeRequired Then
            Dim d As New AddItemToListViewDelegate(AddressOf AddItemToListView)
            Invoke(d, New String() {item})
        Else
            Dim newItem As New ListViewItem(item)
            newItem.SubItems.Add("---")
            ListView1.Items.Add(newItem)
            Application.DoEvents()
        End If
    End Sub
 
    Private Sub ChangeSubItem(ByVal index As Integer, ByVal item As String)
        Try
            If ListView1.InvokeRequired Then
                Dim d As New ChangeSubItemDelegate(AddressOf ChangeSubItem)
                Invoke(d, New Object() {index, item})
            Else
                If ListView1.Items.Count > index Then
                    ListView1.Items(index).SubItems(1).Text = item
                    Application.DoEvents()
                End If
            End If
        Catch
        End Try
    End Sub
 
    Private Sub AddItems()
        Dim x As Long
 
        While x < MAXITEMS
            Try
                AddItemToListView(String.Format("Item #{0}", x))
                x = x + 1
            Catch ex As ThreadAbortException
                Exit While
            End Try
        End While
    End Sub
 
    Private Sub RandomSubItems()
        Dim RNG As New Random
        Dim x As Long
 
        While True
            Try
                ChangeSubItem(RNG.Next(0, MAXITEMS), RNG.Next(0, 1000).ToString())
            Catch ex As ThreadAbortException
                Exit While
            End Try
            ' This little sleep has to be in there for some reason.
            ' The form won't update properly without it!
            Thread.Sleep(1)
        End While
    End Sub
 
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If t1 Is Nothing Then
            Exit Sub
        End If
        t1.Abort()
        t1.Join()
 
        t2.Abort()
        t2.Join()
    End Sub


RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

GeneralRe: Issue updating Listview via Thread Pin
mechman18-Mar-06 6:04
mechman18-Mar-06 6:04 
GeneralRe: Issue updating Listview via Thread Pin
Dave Kreskowiak18-Mar-06 6:24
mveDave Kreskowiak18-Mar-06 6:24 
GeneralRe: Issue updating Listview via Thread Pin
mechman19-Mar-06 7:30
mechman19-Mar-06 7:30 
GeneralRe: Issue updating Listview via Thread Pin
Dave Kreskowiak19-Mar-06 14:22
mveDave Kreskowiak19-Mar-06 14:22 
GeneralRe: Issue updating Listview via Thread Pin
mechman19-Mar-06 14:27
mechman19-Mar-06 14:27 
GeneralRe: Issue updating Listview via Thread Pin
Dave Kreskowiak19-Mar-06 16:51
mveDave Kreskowiak19-Mar-06 16:51 
GeneralRe: Issue updating Listview via Thread Pin
mechman19-Mar-06 18:05
mechman19-Mar-06 18:05 
GeneralRe: Issue updating Listview via Thread Pin
Dave Kreskowiak20-Mar-06 2:48
mveDave Kreskowiak20-Mar-06 2:48 
QuestionMaking a grid on a MDI form or regular form Pin
Quecumber25616-Mar-06 9:03
Quecumber25616-Mar-06 9:03 
Questionhow can i capture printer's jobs Pin
fahad ahsan16-Mar-06 7:34
fahad ahsan16-Mar-06 7:34 
AnswerRe: how can i capture printer's jobs Pin
progload16-Mar-06 7:56
progload16-Mar-06 7:56 
AnswerRe: how can i capture printer's jobs Pin
Duncan Edwards Jones16-Mar-06 9:30
professionalDuncan Edwards Jones16-Mar-06 9:30 
QuestionData Entry Design Pin
MikeUPMC16-Mar-06 6:26
MikeUPMC16-Mar-06 6:26 
QuestionReg: Issue in Crystal Report Pin
sjagans16-Mar-06 5:51
sjagans16-Mar-06 5:51 
QuestionReg: Issue in Crystal Report creation in VB.NET Pin
sjagans17-Mar-06 1:10
sjagans17-Mar-06 1:10 
QuestionHow can I update a listview? Pin
wliong16-Mar-06 4:48
wliong16-Mar-06 4:48 
QuestionLan chat in vb.net Pin
Harshad Pednekar16-Mar-06 0:31
Harshad Pednekar16-Mar-06 0:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.