two worker threads executing parallely but the second one comes into picture during calling a inner function with variables overriden

  • Thread starter Thread starter GUPIEZ
  • Start date Start date
G

GUPIEZ

Guest
Public Sub Fetchdetails()

Dim objDal As New DlDataAccess
Dim intEntityCount As Integer = 0
Dim strUserId As String = String.Empty
Dim strLanguage As String = String.Empty
Dim strXMLInput As New StringBuilder
Dim intBatchTransferCount As Int32 = 0
Dim dsConDetails As New DataSet()
Dim EntityId As String = String.Empty
Dim queueName As String = String.Empty
Dim Endpoint As String = String.Empty



dsConDetails = executing an SP and bringing 2 rows...EntityId,QueueName and EndpointConn

If dsConDetails.Tables.Count > 0 Then

If Not IsNothing(dsConDetails.Tables(0)) Then
intEntityCount = dsConDetails.Tables(0).Rows.Count

If intEntityCount > 0 Then
objGlobal = New WpmsSeRiServiceGlobal
objGlobal.eventSet = False
objGlobal.main = New AutoResetEvent(False)
objGlobal.threadCtr = intEntityCount

m_objEventLog.WriteEntry(intEntityCount.ToString() & " queues found.", EventLogEntryType.Warning)

For Each dr As DataRow In dsConDetails.Tables(0).Rows
EntityId = dr(1).ToString
queueName = dr(2).ToString
Endpoint = dr(3).ToString

CreateWorker(EntityId, queueName, Endpoint)


Next

objGlobal.eventSet = True
objGlobal.main.WaitOne()
Else
m_objEventLog.WriteEntry("No queues found.", EventLogEntryType.Information)
End If
End If

End Sub

Private Sub CreateWorker(ByVal EntityId As String, ByVal queueName As String, ByVal Endpoint As String)
Dim objWorker As New WpmsSeRiWorkerThread
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentDomain.UnhandledException, AddressOf MyHandler
Try

objWorker.[global] = Me.objGlobal
objWorker.ProfilePull(EntityId, queueName, Endpoint) 'This is calling sub in below worker thread
Catch
Throw
Finally
objWorker = Nothing
End Try
End Sub


================================
In WorkerThread.vb file
===========================

Private strEntityId As String = String.Empty
Private strqueue As String = String.Empty
Private strEndpoint As String = String.Empty
Dim objRegKey As RegistryKey
Public objPullprofile As New WpmsSeRiPullProfile
Dim objWpmsCommon As New WpmsCmCommon
Private m_XmlInputNode As XmlNode
Private m_thread As Thread
Dim objEventLog As New EventLog("WpmsSePullService")
Public [global] As WpmsSeRiServiceGlobal
Dim obj_WpmsDeCommonNonDBComponent As New WpmsDeCommonNonDBComponent
Dim format As String = "MM/dd/yyyy HH:mm:ss.fff tt"

Public Sub ProfilePull(ByVal EntityId As String, ByVal queueName As String, ByVal Endpoint As String)
' create a thread start object that refers to our worker...
Dim threadStart As ThreadStart
Me.strEntityId = EntityId
Me.strqueue = queueName
Me.strEndpoint = Endpoint
threadStart = New ThreadStart(AddressOf Me.Start)
'now, create the thread object and start it...
m_thread = New Thread(threadStart)
m_thread.Start()
End Sub


After above code the worked thread is created for 2nd row/connection and hits the start but what happens is before 1st worker thread calls the GetMessage(queueClient) ,the
values/variables are overriden by 2nd worker thread

i.e. strEntityId,strqueue,etc. so when i insert the records in db table for 1st worker thread it enters values of 2nd thread .




Public Sub Start()


'Dim objRegKey As Microsoft.Win32.RegistryKey
Dim ActiveMsgCount As Integer

Try
If Not EventLog.SourceExists("WpmsSePullService") Then
EventLog.CreateEventSource("WpmsSePullService", "WpmsSePullService") ' Create Log
End If
objEventLog.Source = "WpmsSePullService"

Dim queueClient As QueueClient = QueueClient.CreateFromConnectionString(strEndpoint, strqueue)
objRegKey = objWpmsCommon.WpmsCmGetRegistryKey(WpmsTiCmConstants.CNST_REGISTRY_SE_CODE) '("Configuration\SE")

objEventLog.WriteEntry("The process in entering the " & strqueue & "At : " & Now.ToString(format), EventLogEntryType.Information)

Dim nsmgr As NamespaceManager = NamespaceManager.CreateFromConnectionString(strEndpoint)
ActiveMsgCount = Convert.ToInt32(nsmgr.GetQueue(strqueue).MessageCountDetails.ActiveMessageCount)

''Loop through Queue for messages
''While (True)
If ActiveMsgCount > 0 And ActiveMsgCount < 20 Then
For i As Integer = 1 To ActiveMsgCount
Dim message As String = GetMessage(queueClient)
If String.IsNullOrWhiteSpace(message) Then
Exit For
End If
Next
Else
For i As Integer = 1 To 20
Dim message As String = GetMessage(queueClient)
If String.IsNullOrWhiteSpace(message) Then
Exit For
End If
Next
End If
'End While
objEventLog.WriteEntry("Process came out of the queue :" & strqueue & " At :" & Now.ToString(format), EventLogEntryType.Information)

objEventLog.WriteEntry("Process Stopped Successfully for :" & strEntityId, EventLogEntryType.Information)
Catch objErr As WpmsTiErExceptionV2
objEventLog.WriteEntry("Process Stopped In Error for " & strEntityId & ". Error Message is:" & objErr.ErrorMessage, EventLogEntryType.Error)
Catch webex As WebException
objEventLog.WriteEntry("Process Stopped In Error for :" & strEntityId & ". Error Message is:" & webex.Message, EventLogEntryType.Error)
Catch objEx As Exception
objEventLog.WriteEntry("Process Stopped In Error for :" & strEntityId & ". Error Message is:" & objEx.Message, EventLogEntryType.Error)
Finally


SyncLock [global]
[global].threadCtr = [global].threadCtr - 1

End SyncLock
If (([global].threadCtr = 0) And ([global].eventSet = True)) Then
[global].main.Set()

End If


End Try
End Sub

Continue reading...
 
Back
Top