Click here to Skip to main content
15,903,012 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Question on threading Pin
Luc Pattyn16-Nov-10 9:41
sitebuilderLuc Pattyn16-Nov-10 9:41 
GeneralRe: Question on threading Pin
svanwass16-Nov-10 9:50
svanwass16-Nov-10 9:50 
GeneralRe: Question on threading Pin
Luc Pattyn16-Nov-10 11:29
sitebuilderLuc Pattyn16-Nov-10 11:29 
AnswerRe: Question on threading Pin
Tieske817-Nov-10 1:43
professionalTieske817-Nov-10 1:43 
AnswerRe: Question on threading Pin
Spectre_00117-Nov-10 2:38
Spectre_00117-Nov-10 2:38 
GeneralRe: Question on threading Pin
svanwass17-Nov-10 4:52
svanwass17-Nov-10 4:52 
GeneralRe: Question on threading Pin
Spectre_00117-Nov-10 4:56
Spectre_00117-Nov-10 4:56 
AnswerRe: Question on threading [modified] Pin
SLDWorks17-Nov-10 18:59
SLDWorks17-Nov-10 18:59 
This site has been so helpful in the past for me over 8 years. I am self taught .net, and have used it primarily with SolidWorks API since 2003.

Thread for me is simply threads, and delegates. I run threads in order to crunch data. It keep the main form thread alive while crunching the info. Many times the threads have to send stuff back to the main threaded form. In order to do this without cross thread violations, we need to use delegates.

My technique for sending info into a thread, and having the thread call back is rather simple. If I need to create lets say 100 threads. I will use a thread in order to do that. The thread that creates the 100 threads will sleep, only to awake and see if all threads have died. When all threads have died, the main thread cruches and then dies. The main form thread never stalls out, and gets tickled from all sides by all threads from a delegates sub.

Here is the code I wrote to explain the most on these topics. Things to note are as follows:
Each thread can create its node without limitations.
The main thread relys on all threads being done creation. Then the assumtion is made that all thread nodes exist. This can only be true because all threads are not alive. So the main thread changes the display name of each treenode, and then dies itself.

The threads and delegates take object arrays, so you can pack them full of anything, both in and out of the thread.

The form is very much alive through the whole process. If only a form could speak, because this one is far from a deep sleep.

Cheers,
Sean P
(Sldprt[ItsAboutTime])


Imports System.Threading

Public Class Form1

    Dim t1 As Thread
    Dim t2 As Thread()
    Dim c As Collection

    Dim treeview As TreeView

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


        tMain()


    End Sub

    Sub tMain()

        c = New Collection

        treeview = New TreeView
        Me.Controls.Add(treeview)
        treeview.Size = New Size(200, 400)
        treeview.Location = New Point(10, 6)
        treeview.Visible = True

        c.Add(100, "StartData")

        t1 = New Thread(AddressOf MainThreading)

        t1.Start(New Object() {c})

    End Sub

    Sub MainThreading(ByVal args As Object)

        With CType(args(0), Collection)

            ReDim t2(.Item("StartData"))

            Dim i As Integer

            For i = 0 To UBound(t2)

                .Add(i, "ThreadKey" & i)

                t2(i) = New Thread(AddressOf WorkerThread)

                t2(i).Start(New Object() {args(0), i})

            Next

            Do

                Dim bol As Boolean = True

                For i = 0 To UBound(t2)

                    If t2(i).IsAlive Then

                        bol = False

                    End If

                Next

                If bol Then

                    Exit Do

                End If

                Thread.Sleep(100)

            Loop

            Dim treenode As TreeNode

            For i = 0 To .Item("StartData")

                With treeview.Nodes.Item("Nod" & i)

                    ChangeNodes(New Object() {treeview.Nodes.Item("Nod" & i), _
                                                    .Text & _
                                              " [" & .Name & _
                                              " of " & .Tag & "]"})

                End With

            Next

        End With

    End Sub

    Sub WorkerThread(ByVal args As Object())

        Dim treenode As TreeNode = New TreeNode

        Dim CollectionCount As Integer = CType(args(0), Collection).Item("StartData")
        Dim Name As String = CType(args(1), String)

        treenode.Name = "Nod" & Name
        treenode.Text = Name
        treenode.Tag = CollectionCount

        AddNodes(treenode)

    End Sub

    Public Delegate Sub AddNode(ByRef obj As Object)

    Sub AddNodes(ByRef obj As Object)

        If treeview.InvokeRequired Then

            Dim ad As AddNode = New AddNode(AddressOf AddNodes)

            treeview.Invoke(ad, obj)

        Else

            treeview.BeginUpdate()

            treeview.Nodes.Add(obj)

            treeview.EndUpdate()

        End If

    End Sub

    Public Delegate Sub ChangeNode(ByRef obj As Object)

    Sub ChangeNodes(ByRef obj As Object)

        If treeview.InvokeRequired Then

            Dim ad As ChangeNode = New ChangeNode(AddressOf ChangeNodes)

            treeview.Invoke(ad, obj)

        Else

            Dim TreeNode As TreeNode = CType(obj(0), TreeNode)

            TreeNode.Text = CType(obj(1), String)

        End If

    End Sub

End Class

Dont turn around, cause your...
modified on Thursday, November 18, 2010 1:06 AM

QuestionHow to populate a listbox from one listbox? Pin
waner michaud12-Nov-10 6:59
waner michaud12-Nov-10 6:59 
AnswerRe: How to populate a listbox from one listbox? Pin
Dr.Walt Fair, PE12-Nov-10 9:58
professionalDr.Walt Fair, PE12-Nov-10 9:58 
AnswerRe: How to populate a listbox from one listbox? Pin
Tarun.K.S15-Nov-10 19:28
Tarun.K.S15-Nov-10 19:28 
QuestionMulti-project solution with common DLLs Pin
vvincent12-Nov-10 5:17
vvincent12-Nov-10 5:17 
AnswerRe: Multi-project solution with common DLLs Pin
Dave Kreskowiak12-Nov-10 6:16
mveDave Kreskowiak12-Nov-10 6:16 
GeneralRe: Multi-project solution with common DLLs Pin
vvincent12-Nov-10 8:54
vvincent12-Nov-10 8:54 
GeneralRe: Multi-project solution with common DLLs Pin
Dave Kreskowiak12-Nov-10 10:14
mveDave Kreskowiak12-Nov-10 10:14 
GeneralRe: Multi-project solution with common DLLs Pin
vvincent12-Nov-10 11:27
vvincent12-Nov-10 11:27 
AnswerRe: Multi-project solution with common DLLs Pin
_Erik_12-Nov-10 6:38
_Erik_12-Nov-10 6:38 
QuestionStrangest problem with interop on Win7 client and .ocx control Pin
Jon_Boy12-Nov-10 3:38
Jon_Boy12-Nov-10 3:38 
AnswerRe: Strangest problem with interop on Win7 client and .ocx control Pin
Jon_Boy15-Nov-10 4:19
Jon_Boy15-Nov-10 4:19 
QuestionPublish the vb.net application for only one user Pin
vijay248211-Nov-10 19:56
vijay248211-Nov-10 19:56 
AnswerRe: Publish the vb.net application for only one user Pin
DaveAuld11-Nov-10 20:10
professionalDaveAuld11-Nov-10 20:10 
GeneralRe: Publish the vb.net application for only one user Pin
vijay248211-Nov-10 23:10
vijay248211-Nov-10 23:10 
QuestionSystem.Timers.Timer Event Handling with Web Message Box Pin
ggoutam711-Nov-10 18:09
ggoutam711-Nov-10 18:09 
AnswerRe: System.Timers.Timer Event Handling with Web Message Box Pin
Dave Kreskowiak12-Nov-10 3:40
mveDave Kreskowiak12-Nov-10 3:40 
GeneralRe: System.Timers.Timer Event Handling with Web Message Box Pin
ggoutam715-Nov-10 20:26
ggoutam715-Nov-10 20:26 

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.