Home All Groups Group Topic Archive Search About

Multithreaded application, thread not quiting.

Author
14 Nov 2006 4:42 PM
Mitch Wardrop
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:\uploader"
     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.Threading.Thread(AddressOf
Work.Connect)
End Module

Filename : Main.vb

Imports System.Threading
Public Class Main
     Private Sub Connect_Button_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Connect_Button.Click
         variable.connect.Start()
     End Sub
End Class

Filename : disconnect.vb

Imports System.Threading
Public Class disconnect_form
     Private Sub Yes_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Yes.Click
         variable.connect.kill()
         Me.Close()
     End Sub
End Class

Author
14 Nov 2006 4:57 PM
Robinson
> Imports System.Threading
> Public Class disconnect_form
>     Private Sub Yes_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Yes.Click
>         variable.connect.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".