Click here to Skip to main content
15,891,431 members
Home / Discussions / Visual Basic
   

Visual Basic

 
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 
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 
OK. It works fine...

Form1 code:
    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 Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Module1.myForm = Me
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListView1.Items.Clear()
        t1 = New Thread(New ThreadStart(AddressOf AddItems))
        t1.Start()
        Thread.Sleep(100)
        t2 = New Thread(New ThreadStart(AddressOf Module1.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
 
    Public 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
 
    Public 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 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

Module1 code:
Imports System.Threading
 
Module Module1
    Public myForm As Form1
    Public Const MAXITEMS As Long = 20
 
    Public Sub RandomSubItems()
        Dim RNG As New Random
        Dim x As Long
 
        While True
            Try
                myForm.ChangeSubItem(RNG.Next(0, MAXITEMS), RNG.Next(0, 1000).ToString())
            Catch ex As ThreadAbortException
                Exit While
            End Try
            Thread.Sleep(1)
        End While
    End Sub
End Module

I never use Modules, ... ever. They're an outdated concept that's around just to be backwards-compatible with VB6 code.


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

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 
AnswerRe: Lan chat in vb.net Pin
albCode16-Mar-06 1:40
albCode16-Mar-06 1:40 
QuestionHow to use MSChart in VB.Net Web Application?? Pin
DreamQuestioner16-Mar-06 0:02
DreamQuestioner16-Mar-06 0:02 

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.