Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

I thought Threading was simple.

Create a new thread which minds its own business. I never thought twice about it having its own variables...

 

In my class I have a number of private variables as well as a few private threads (both common threads and timer threads). I want the threads and class to share the private variables.

Shouldn't be too hard, IMHO....

 

Regards

/Kejpa

  • Administrators
Posted

Threading isn't difficult as such but can get complex if variables need to be accessed by multiple threads - you will probably need to investigae the Monitor class.

Also depending on your language have a look at either lock (C#) or SyncLock(VB).

 

If you give a bit more detail and / or post some code we may be able to help a bit more.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Hey there,

I never found threading very hard until I tried accessing class level variables...

:)

 

Here's some stripped code for you

Public Class Class1
' Removed Private variables used for Property Get/Set 

Private imodModel As Integer				 ' External interface model
Private rmodTimeStamp As Single				 ' Timestamp for updated data
Private fThreadIsAlive As Boolean			 ' Flag for Reader thread loop
Private baSensorProperties As System.Collections.BitArray = New System.Collections.BitArray(23)	' Property update flags
Private tReader As Threading.Thread			 ' External reader thread
Private oPropertyTimer As Threading.Timer	 ' Property reader thread

Public Sub New()
	' Created property reader thread
	oPropertyTimer = New Threading.Timer(New Threading.TimerCallback(AddressOf oPropertyTimer_OnTimer), 0, 10000, 30000)
	' Created main IO reader in new thread
	tReader = New Threading.Thread(AddressOf Reader)
	fThreadIsAlive = True
	tReader.Start()
End Sub

Private Sub oPropertyTimer_OnTimer(ByVal Dummy As Object)
	Dim baCurrent As New BitArray(23)

	If Not Equals(baCurrent, baSensorProperties) Then
		' Tell the world we've got some new properties for the IO
		RaiseEvent PropertyUpdate(Properties)
	End If

End Sub

Private Sub Reader()	' General IO reader thread
	Dim _data As cmsg			' Message structure
	Dim lRet As Integer = 0		' General return recipient
	Dim data_len As Integer		' Lenght of received structure

	Do
		data_len = 1
		Try
			lRet = ntdll.ReadIO(imodHandle, _data, data_len, 0)
			If lRet = 0 Then				  ' Receive Ok
				Dim iLen As Byte = _data.length And &H1F
				If ((iLen > 0) And (iLen <= 8)) Then
					' Analyze received data
					DecodeSyncResult(_data.data)
				End If
			End If
		Catch err As Exception
			' Just for development!
			Console.WriteLine(err.GetBaseException.ToString)
		End Try
	Loop While fThreadIsAlive

End Sub
Private Sub DecodeSyncResult(ByVal _Message() As Byte)
	' Analyze message and update data
	Dim iRawValues(2), iReturnValues() As Integer

	iRawValues(0) = CInt(_Message(0)) + CInt(_Message(1)) * 256
	iRawValues(1) = CInt(_Message(2)) + CInt(_Message(3)) * 256
	iRawValues(2) = CInt(_Message(4)) + CInt(_Message(5)) * 256
	iRawValues(2) = CInt(_Message(6)) + CInt(_Message(7)) * 256

	Select Case imodModel	   ' Depending on the model different values will be exposed 
		Case 10
			ReDim iReturnValues(2)
			iReturnValues(0) = iRawValues(0)
			iReturnValues(1) = iRawValues(1)
			iReturnValues(2) = iRawValues(3)

			Console.WriteLine(iReturnValues(0) & vbTab & iReturnValues(1) & vbTab & iReturnValues(2))
		Case 20
			ReDim iReturnValues(1)
			iReturnValues(0) = iRawValues(0)
			iReturnValues(1) = iRawValues(3)

			Console.WriteLine(iReturnValues(0) & vbTab & iReturnValues(1))
		Case Else
	End Select
	If Not iReturnValues Is Nothing Then
		' Tell the world we've got new data and at what time we got it along with all properties of the object
		RaiseEvent NewData(iReturnValues, imodTimeStamp, Me)
	End If

End Sub

Protected Overrides Sub Finalize()
	' Dispose all used objects
	If Not tReader Is Nothing Then
		fThreadIsAlive = False
		tReader.Abort()
	End If
	tReader = Nothing

	oPropertyTimer.Dispose()
	oPropertyTimer = Nothing
End Sub
End Class

It's in the Reader sub when I was trying to get imodModel and imodTimestamp I stumbled over the problems with non-accessable variables.

 

TIA

/Kejpa

Posted
Hey there,

I never found threading very hard until I tried accessing class level variables...

:)

Sorry for bothering you with this... :o

I've found it my self, it's working as I thought all the time, it's just that I was over writing a buffer and so I never got the request for model down to the external I/O. Changing that made it work swell

The timestamp was 0 because it was never updated...

 

Regards

/Kejpa

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...