vb.net multithreading -

  • Thread starter Thread starter Steven Thomas
  • Start date Start date
S

Steven Thomas

Ok
I have a multithreaded windows service application. This application
serves reports back to multiple web applications. This service can
run from 1-x reports (on different threads) at any given time. My
problem is once I start a thread running, if I need to abort the
thread, I don't know how to referance it?

My code:
Private Sub StartReportServer()
Try
' DIM MY LOCAL VARIABLES
......
' READ THE CONFIG FILE
.....
While IsRunning
'LOOP THROUGH THE ARRAY OF AVAILABLE THREADS YOU HAVE TO SERVICE
While c < threads
' CHECK TO SEE IF THIS THREAD IS RUNNING
If arThreadCnts(3, c) = 0 Then
'IF IT IS NOT RUNNING CHECK TO SEE IF THERE IS ANY
REPORTS WAITING BY POLLING THE QUEUE
dtReportToRun = cFunctions.polldb()
' IF THERE IS WORK TO BE DONE READ THE DATA
FROM THE REPORTQUEUE
.....
' IF THE REPORT TYPE IS ACCESS CREATE SNAPSHOT
If vOutputFileType = "MSACCESS" Then
Dim cAccessFunc As New AccessFunc()
'ADD A HANDLER TO CAPTURE DATA BACK
FROM THE THREAD
AddHandler cAccessFunc.ThreadDone,
AddressOf RptMSAThreadDone
Dim othread1 As New Thread(AddressOf
cAccessFunc.CAccessSnapShot)
' SET THE PROPERTIES OF THE CLASS
AND START NEW THREAD
.....
' START THREAD
othread1.start()
Else
'DO THE SAME FOR EXCEL
Dim cExcelFunc As New ExcelFunc()
End If
End If
Else
' CHECK STATUS OF REPORT TO SEE IF IT IS
TIMED OUT THEN ABORT THREAD
dtReportStatus = cFunctions.CheckRptStatus(
arQueues(x), CLng(arThreadCnts(3, c)))
If CStr(dtReportStatus.Rows(0).Item
("ProcessingState")) = "T" Then
***** HERE I WANT TO ABORT THE THREAD IF IT HAS BEEN SET
***** TO A STATUS OF T FROM THE FRONT END
End If
End If
c += 1
End While
c = 0
x += 2
End While
c = 0
x = 1
Thread.CurrentThread.Sleep(1000)
End While
End Try
End Sub
-----------------------------------------------------------
 
Steven,

Again you have a problem with object references. You should maintain an
array or collection of thread objects. Adding a new thread to the
collection as you need them. This way you can reference them later and
issue the myArrays(i).Abort if need be...

Regards,
Dan
 
Dan,
Thanks for the input, but I am not sure how to go about creating this
array of threads so I can referance them. After looking at my code
snippet, where would I make the change so I would have an array of
threads?

Thanks in advance for your help.


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Steven,
you could use an array or a collection below is some untested/pseudo code
but you will get the idea

Dim col() as New Collection

' your code is here

Dim othread1 As New Thread(AddressOf cAccessFunc.CAccessSnapShot)

col.Add ("WhatEverKeyYouWouldLikeToUse", othread1)

' the rest of your code


Now if you need to reference you rthread later to abort, you would:

Dim aThread as Thread = col.Item("WhatEverKeyYouWouldLikeToUse")

aThread.Abort()
 
Dim col() as New Collection

Just to point out a small typo in your code. This line creates an array of
collections. It should be:

Dim col as New Collection 'Note the lack of parentheses
 
Back
Top