Best method to kill a thread

andresleon

Member
Joined
Jun 22, 2005
Messages
9
Programming Experience
1-3
Hello All,
I have a windows service that creates and starts a separate thread to perform a WMI query to a remote computer. I must perform this query to several dozen computers to determine the status of each station (available for use, in use (somepne logged on), or down/powered off)

This is the code i use to create and start the thread:

VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] logon [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] RemoteLogon[/size]
intThreadTimeOutInMilli = 500
[size=2][color=#0000ff]Dim[/color][/size][size=2] logonthread [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] Thread([/size][size=2][color=#0000ff]AddressOf[/color][/size][size=2] logon.getRemoteStatus)
logon.strComputerIP = strStationIP [/size][size=2][color=#008000]'pass the machine ip to query
[/color][/size][size=2]logon.adminUsername = strWMIUserName [/size][size=2][color=#008000]'pass the username that has wmi access rights to machine
[/color][/size][size=2]logon.adminPwd = strWMIPassword [/size][size=2][color=#008000]'pass the password of username that has wmi access rights to machine
[/color][/size][size=2]logonthread.Name = strPCname & "_ScanID_" & intCurrentScanID
logonthread.Start() [/size][size=2][color=#008000]'start thread
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] intTimeOutCounter [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size]
 
[size=2][color=#0000ff]
[/color][/size][size=2][color=#0000ff]Do [/color][/size][size=2][color=#0000ff]While[/color][/size][size=2] logonthread.Join(intThreadTimeOutInMilli * 2) = [/size][size=2][color=#0000ff]False[/color][/size]
[size=2][color=#008000]'loop while thread has not ended...
[/color][/size][size=2]Thread.Sleep(intThreadTimeOutInMilli)[/size]
[size=2]intTimeOutCounter += 1
[/size][size=2][color=#0000ff]If[/color][/size][size=2] intTimeOutCounter > 2 [/size][size=2][color=#0000ff]Then
	 [/color][/size][size=2]logonthread.Abort()
	 EventLogger("NO ERROR waited for thread to end", logonthread.Name.ToString() & " state is " & _
		logonthread.ThreadState.ToString())
	 intTimeOutCounter = 0
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]Loop
[/color][/size][size=2][color=#0000ff]
[/color][/size]

My problem is that if a machine is not responding, the thread runs for over 40 seconds until it is finally terminated by the system. The DO WHILE loop i created was meant to kill the thread after about 1 or 2 seconds. But it doesnt work... the thread still attempts to finish. Then, after about 35 to 40 secs, the sub called by the thread finally raises error 5 and ends.

Is there a way to abort the thread quicker and with more control? Hope what i have explained makes sense. If not, please reply so i can answer your questions.

Thanks in advanced!!!
 
Actually what can i understand is that , though u seem to be doing right but actually after calling the abort method, system caution the thread (login thread) to terminate but i suppose u have created a send request to remote host in that method, which suspend its execution to another thread thats why it is unable to abort until system times out.
but this is just an idea u can check by debugging , if it stops there then it does mean i m right :)
 
Back
Top