Multithreaded application, thread not quiting.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mitch Wardrop

    #1

    Multithreaded application, thread not quiting.

    ok here it goes I must be doing something way off but it makes sense to
    me just not to VB.NET which would be a huge problem. Anyways I'll
    explain what I am doing than give code examples. basicly I've declared a
    thread in a module file where all my variables are declared for the
    enitre application, than it calls the thread from the main form and runs
    a sub routine located in a different class (I call it "work" class, this
    is where all the code that actually does anything is stored), the thread
    executes and begins dumping database data no problem, then when I open
    up my disconnect dialogue window and click yes to disconnect and tell my
    thread to quit it never quites in fact it just hangs the program, not
    sure why. Anyways heres the code I am using if anyone can tell me what
    is going on I would appreciate it.

    file name : variables.vb

    Module variable
    'Define Global Variables
    Public database As String = "WELLDAQ\DBINFO "
    Public dbusername As String = "******"
    Public dbpassword As String = "******"
    'job is declared by the work.activejob( ) routine called from the
    start form
    Public job As String
    Public jobid As Integer
    Public dumplocation As String = "C:\uploade r"
    Public jobtype As String
    Public server As String = "dkdserver.ath. cx"
    Public serverusername As String = "******"
    Public serverpassword As String = "******"
    Public disconnect As Boolean = False
    Public active As Boolean = False
    Public company As String
    Public dumpcount As Integer
    Public uploadcount As Integer
    Public connect = New Global.System.T hreading.Thread (AddressOf
    Work.Connect)
    End Module

    Filename : Main.vb

    Imports System.Threadin g
    Public Class Main
    Private Sub Connect_Button_ Click(ByVal sender As System.Object,
    ByVal e As System.EventArg s) Handles Connect_Button. Click
    variable.connec t.Start()
    End Sub
    End Class

    Filename : disconnect.vb

    Imports System.Threadin g
    Public Class disconnect_form
    Private Sub Yes_Click(ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Yes.Click
    variable.connec t.kill()
    Me.Close()
    End Sub
    End Class
  • Robinson

    #2
    Re: Multithreaded application, thread not quiting.

    Imports System.Threadin g
    Public Class disconnect_form
    Private Sub Yes_Click(ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Yes.Click
    variable.connec t.kill()
    Me.Close()
    End Sub
    End Class
    Using Kill is very bad practice, especially if your thread is handling
    unmanaged objects (or managed objects that are wrappers for unmanaged
    objects), particularly any out of process COM objects - you can get all
    kinds of strange and wonderful errors/exceptions/problems by doing this.
    You should stop the thread by checking a bCancel flag inside the thread loop
    and then invoking a "completed" delegate on the form. Before setting the
    bCancel flag in your Yes_Click handler, you should set a "bClosing" flag, so
    you know to close the form when the thread invokes "completed" .






    Comment

    Working...