Threading Questions (repost)

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

- For cleanup, is it sufficient to set a Thread to Nothing after it's done?

- It is OK to pass objects out of the thread? (dumb question maybe but I
want to be sure)

- What's the best way to process messages coming out of a thread? I want to
queue them up, but MessageQueue doesn't look like what I need. Should I just
make my own queue class? If so I'll have to worry about enumerator
synchronization... a pointer to a 'best practice' example would be very
helpful.

Bob
 
Re 3rd question - The way I queue results from multiple worker threads is
shown below. A typical use of this is to launch threads from a form and
periodically poll the queue in the form via a timer control. Alternatively,
package this in a class that also declares an event, and raise the event when
date is enqueued. (caution re threading, forms, and marshalling.) An array
of objects is handy because you can enqueue pretty much anything that way.
Substitute whatever you want for 'xxx'.

Sample uses:
xxxEnqueue("stuff",1.5,true)
dim Data() as object = xxxDequeue()
' data(0)="stuff", data(1)=1.5, and data(2)=true

Private xxxQueue As New Collections.Queue

Public Sub xxxEnqueue(ByVal ParamArray Data() As Object)
' thread-safely enqueue an array of objects
Dim bAction As Boolean = False ' true if we have something to enqueue
If Not Data Is Nothing Then _
If Data.Length > 0 Then _
If TypeName(Data(0)) = "String" Then _
bAction = True
If bAction Then
Threading.Monitor.Enter(xxxQueue)
HnetQueue.Enqueue(Data)
Threading.Monitor.Exit(xxxQueue)
End If
' raise an event here if you want to
End Sub

Public Function xxxDequeue() As Object()
' thread-safely dequeue and return an array of objects
Dim Data() As Object ' queue entry
Threading.Monitor.Enter(xxxQueue)
If xxxQueue.Count > 0 Then
Data = CType(xxxQueue.Dequeue(), Object())
Else
Data = Nothing
End If
Threading.Monitor.Exit(xxxQueue)
Return Data
End Function
 
I was too hasty in the first reply. Also change

Sample uses:
xxxEnqueue("stuff",1.5,true)
dim Data() as object = xxxDequeue()
' data(0)="stuff", data(1)=1.5, and data(2)=true

to

Sample uses:
dim zzz() as object = {"stuff",1.5,true}
xxxEnqueue(zzz)
dim Data() as object = xxxDequeue()
' data(0)="stuff", data(1)=1.5, and data(2)=true
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Threading question.... 4
Queue Thread Safe question 2
Collection modifying problem 7
Threading..... 4
synclock questions 12
Threading in .Net... 4
Response.Write from VB.NET class? 1
Parallel Programming 3

Back
Top