Hi,
I need to use timers in multithreaded windows service. If any of the thread is alive then timer should not start. I want to start once all the threads process are completed and to dispose the threads. Please any one help me....here's is my code..
Public Class Service1
Imports System.Threading
Public Class Service1
Protected Overrides Sub OnStart(ByVal args() As String)
Protected Overrides Sub OnStop()
Public Sub ReceiveThreadMessage(ByVal ThreadIndex As Integer, ByVal wPoolId As Integer, ByVal Counter As Integer)
End Class
#Class FIle :ClsThread.vb
Public Sub StartThread()
End Class
I need to use timers in multithreaded windows service. If any of the thread is alive then timer should not start. I want to start once all the threads process are completed and to dispose the threads. Please any one help me....here's is my code..
Public Class Service1
Imports System.Threading
Public Class Service1
Dim arrWorkers As clsThread()
Dim workerThreads As Thread()
Public m_ThreadList As New ArrayList
Dim numberOfThreads As Integer = 3
Dim workerThreads As Thread()
Public m_ThreadList As New ArrayList
Dim numberOfThreads As Integer = 3
Protected Overrides Sub OnStart(ByVal args() As String)
arrWorkers = New clsThread(numberOfThreads) {}
workerThreads = New Thread(numberOfThreads) {}
For i As Integer = 0 To 3
m_ThreadList.Add(Nothing)
arrWorkers(i) = New clsThread(i, 20, Me)
Dim st As New ThreadStart(AddressOf arrWorkers(i).StartThread)
workerThreads(i) = New Thread(st)
workerThreads(i).Start()
m_ThreadList.Item(i) = st
Next
End SubworkerThreads = New Thread(numberOfThreads) {}
For i As Integer = 0 To 3
m_ThreadList.Add(Nothing)
arrWorkers(i) = New clsThread(i, 20, Me)
Dim st As New ThreadStart(AddressOf arrWorkers(i).StartThread)
workerThreads(i) = New Thread(st)
workerThreads(i).Start()
m_ThreadList.Item(i) = st
Next
Protected Overrides Sub OnStop()
'If Not m_ThreadList(0) Is Nothing Then
' If CType(m_ThreadList(0), Thread).IsAlive Then
' CType(m_ThreadList(0), Thread).Abort()
' m_ThreadList(0) = Nothing
' Else
' _createErrorFile(Reflection.MethodInfo.GetCurrentMethod.Name & "No thread is active in this section")
' End If
'Else
' _createErrorFile(Reflection.MethodInfo.GetCurrentMethod.Name & "No thread is active in this section")
'End If
End Sub' If CType(m_ThreadList(0), Thread).IsAlive Then
' CType(m_ThreadList(0), Thread).Abort()
' m_ThreadList(0) = Nothing
' Else
' _createErrorFile(Reflection.MethodInfo.GetCurrentMethod.Name & "No thread is active in this section")
' End If
'Else
' _createErrorFile(Reflection.MethodInfo.GetCurrentMethod.Name & "No thread is active in this section")
'End If
Public Sub ReceiveThreadMessage(ByVal ThreadIndex As Integer, ByVal wPoolId As Integer, ByVal Counter As Integer)
Dim all_threads_completed As Boolean = False
m_ThreadList(ThreadIndex - 1) = Nothing
'For n As Integer = 0 To workerThreads.Count - 1
' If n = ThreadIndex Then
' workerThreads(ThreadIndex).Abort()
' End If
' If workerThreads
Is Nothing Then
' all_threads_completed = True
' Else
' all_threads_completed = False
' Exit For
' End If
'Next
workerThreads(ThreadIndex).Abort()
End Subm_ThreadList(ThreadIndex - 1) = Nothing
'For n As Integer = 0 To workerThreads.Count - 1
' If n = ThreadIndex Then
' workerThreads(ThreadIndex).Abort()
' End If
' If workerThreads
' all_threads_completed = True
' Else
' all_threads_completed = False
' Exit For
' End If
'Next
workerThreads(ThreadIndex).Abort()
End Class
#Class FIle :ClsThread.vb
Private m_ThreadIndex As Integer
Private m_WorkPoolId As Integer
Private m_Counter As Integer = 0
Public Shared objCompletedComputationsLock As New Object
Private m_Args(2) As Object
Private m_MainWindow As Service1
Public Event ThreadDone(ByVal strsuccess As String)
Private Delegate Sub NotifyMainWindow(ByVal ThreadIndex As Integer, ByVal wPoolId As Integer, ByVal Counter As Integer)
Private m_NotifyMainWindow As NotifyMainWindow
Public Sub New(ByVal ThreadIndex As Integer, ByVal WorkPoolId As Integer, ByRef MainWindow As Service1)Private m_WorkPoolId As Integer
Private m_Counter As Integer = 0
Public Shared objCompletedComputationsLock As New Object
Private m_Args(2) As Object
Private m_MainWindow As Service1
Public Event ThreadDone(ByVal strsuccess As String)
Private Delegate Sub NotifyMainWindow(ByVal ThreadIndex As Integer, ByVal wPoolId As Integer, ByVal Counter As Integer)
Private m_NotifyMainWindow As NotifyMainWindow
m_ThreadIndex = ThreadIndex
m_MainWindow = MainWindow
m_WorkPoolId = WorkPoolId
'We need to point our delegate to the Method, which we want to call from this thread
m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage
End Subm_MainWindow = MainWindow
m_WorkPoolId = WorkPoolId
'We need to point our delegate to the Method, which we want to call from this thread
m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage
Public Sub StartThread()
While True
' SyncLock objCompletedComputationsLock
m_Counter = m_Counter + 1
ReDim m_Args(2)
m_Args(0) = m_ThreadIndex
m_Args(1) = m_WorkPoolId
m_Args(2) = m_Counter
m_MainWindow.ReceiveThreadMessage(m_Args(0), m_Args(1), m_Args(2))
'm_MainWindow.Invoke(m_NotifyMainWindow, m_Args)
'wait for some time before continuing loop
Thread.Sleep(200000)
' End SyncLock
End While
End Sub' SyncLock objCompletedComputationsLock
m_Counter = m_Counter + 1
ReDim m_Args(2)
m_Args(0) = m_ThreadIndex
m_Args(1) = m_WorkPoolId
m_Args(2) = m_Counter
m_MainWindow.ReceiveThreadMessage(m_Args(0), m_Args(1), m_Args(2))
'm_MainWindow.Invoke(m_NotifyMainWindow, m_Args)
'wait for some time before continuing loop
Thread.Sleep(200000)
' End SyncLock
End While
End Class