468,747 Members | 1,700 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,747 developers. It's quick & easy.

Threading Questions (repost)

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
Nov 21 '05 #1
4 1137
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

"Bob" wrote:
- 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

"Bob" wrote:
- 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

Nov 21 '05 #2
edit correction - replace hnet with xxx in the previous post

"AMercer" wrote:
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

"Bob" wrote:
- 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


"Bob" wrote:
- 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

Nov 21 '05 #3
edit correction - replace hnet with xxx in the previous post

"AMercer" wrote:
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

"Bob" wrote:
- 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


"Bob" wrote:
- 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

Nov 21 '05 #4
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

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

65 posts views Thread by Anthony_Barker | last post: by
4 posts views Thread by Antal Rutz | last post: by
3 posts views Thread by Phillip N Rounds | last post: by
9 posts views Thread by akrapus | last post: by
4 posts views Thread by DBC User | last post: by
126 posts views Thread by Dann Corbit | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.