Tread help needed with displaying data on a form with a stop button.

I have tried to figure out what I am doing wrong by reading articles, but I am stuck.  I am trying to create a thread that will display data in an infine loop until a stop button is pressed.  It seams the form just exits unless I make it synchronous which disables using the stop button.  I will include some code I have below:

Public Class Form2
    Dim blFormOpen As Boolean
    Dim thrLoop As Thread

    Private Sub Fill_Form(ByVal intId As Integer)
   'uses the ID to write that data to form fields
   end sub

    Public Sub Load_Form(ByVal sender As System.Object, ByVal e As System.EventArgs)
        blFormOpen = True
       'reads database information into a datagrid
        thrLoop = New Thread(AddressOf Display_Loop)
        thrLoop.IsBackground = True
        thrLoop.Start()
    end sub
   
    Public Delegate Sub Display_Loop_Delegate()

    Public Sub Display_Loop()
        Dim i As Integer

        intSleep = FForm1.txtTrans.Text * 1000
        If Me.InvokeRequired Then
            Dim displayloopdelegate As Display_Loop_Delegate
            displayloopdelegate = AddressOf Display_Loop
            Invoke(displayloopdelegate)
            Exit Sub
        End If
        While blFormOpen
            If CountLive > 0 Then
                For i = 0 To CountLive - 1
                    If blFormOpen Then
                        txtAuction.Text = "Live Auction"
                        Fill_Form(dstDev.Tables("Live").Rows(i)("PackageID"))
                        FForm2.Refresh()
                        FForm2.Update()
                        Thread.Sleep(intSleep)
                    Else
                        Exit Sub
                    End If
                Next
            End If
            If CountSilent > 0 Then
                For i = 0 To CountSilent - 1
                    If blFormOpen Then
                        txtAuction.Text = "Silent Auction"
                        Fill_Form(dstDev.Tables("Silent").Rows(i)("PackageID"))
                        FForm2.Refresh()
                        FForm2.Update()
                        Thread.Sleep(intSleep)
                    Else
                        Exit Sub
                    End If
                Next
            End If
        End While
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        thrLoop.Abort()
        thrLoop = Nothing
        blFormOpen = False
        FForm2.Hide()
        FForm1.Show()
        FForm1.BringToFront()
        FForm1.Focus()
    End Sub
questincAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Study my example here:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21729609.html

Basically you encapsulate the thread in a class (passing in anything into the class as need be) then make the class raise custom events.  The main UI traps those events and uses Delegates and Invoke() to marshal the call from the encapsulated thread onto the main UI thread so you can safely update the GUI.
0
 
sk33v3Commented:
Ok at the top put a declaration to a boolean Variable

Dim BlnStop as Boolean

Now inside your thread worker process put the follwoing

SyncLock
     if BlnStop then
          blFormOpen=false
     end if
End SyncLock

Now inside your stop command button put the following

SyncLock
     BlnStop=True
End SyncLock

Try that.
0
 
questincAuthor Commented:
I tried using SyncLock with an Object I defined in the from with BlnStop and it is still closing the form soon after the thread starts.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
sk33v3Commented:
Ok, so what code is executing the closing of the form? Also which form is closing? You are referencing a few forms in your code.
0
 
questincAuthor Commented:
This is what I am confused about.  I have two forms, with the first just collecting parameters and should not end until an exit button on it is pressed.  By tracing how it is currently working in the debugger it seems to not get past the Invoke(displayloopdelegate) line.  The whole application seems to end then and I am not sure of any line that would cause this.
0
 
mydasxCommented:
kk.
gotcha.
Dude goto an event driven model.  If you raise a custom event in your thread that posts your data in it, the main form or what ever form can handle this event and update the interface.  also handle the button click event, which will simple flip the boolean in the threads loop.
0
 
questincAuthor Commented:
I am unsure of how to do that now, but I will start researching it.  If you have any helpful links they would be appreciated.
0
 
questincAuthor Commented:
I am not sure how events help in this situation because I think they are still used in threads and my threads are causing the application to exit.
0
 
mydasxCommented:
please describe the displaying data in an infinite loop part.  what are you trying to accomplish?
0
 
mydasxCommented:
i.e. i'm trying understand why you went the direction you went.
0
 
questincAuthor Commented:
I am writing this program to display auction information (items bid on, who one, bid amount, description, etc) and it will go from the beginning item that matched the parameters on the first form to the end item and continue that loop until it is stopped.  I am getting the IDs of what matched the inputs the first time before the loop starts, and then getting the updated information from the database.  I am open to another direction, but I do need a loop to some degree, but I would like to be able to stop it without using "End Task".
0
 
questincAuthor Commented:
I would love to firgure this problem out anyway, but if anyone has some good tips I could use advise today because I am going to have to have this run with or without a stop button today (6/1/2006).
0
 
questincAuthor Commented:
I am beginning to think this is exiting because I am string to start the Form2 thread in a public subroutine I am calling from Form1.  I am just not sure of how to load the information I want and get everything started from a Start button on Form1.  I am confused as to why it is exiting.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Show us how you are opening Form2 from Form1...
0
 
questincAuthor Commented:
I have figured it out with all your help.  The timer helped a lot and I think when I was doing FForm1.hide before FForm2.show that was causing problems so I got rid of that line.  I am now able to start, stop, and restart it.  Thank you for all your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.