473,489 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1282
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
6657
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
4
1573
by: Antal Rutz | last post by:
Hi, All! I'm new to threading. I have some design questions: Task: I collect data and store them in an RDBMS (mysql or pgsql) The question is how to do that with threading? The...
1
1579
by: Jon Sequeira | last post by:
I'm have a class that represents shipping cost data for a commerce web site. The underlying data may only change once a month, maybe less, so I'd rather not hit the database every time the class is...
77
5207
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
3
1837
by: Phillip N Rounds | last post by:
Why is this using 50% of available CPU? What I am trying to accomplish in this service is as follows: In the main class (COS_Service) , int Main(), initializes a timer. Each OnTimer()...
3
5954
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
9
2162
by: akrapus | last post by:
Hi, I am trying to understand how to use threading in Python. I get threading as a concept, but not the implementation. In order to start threading, do you call it as a separate function,...
4
321
by: DBC User | last post by:
I have a background process which reads a table to see if there are any pending requests. If there are any, then it will start a worker thread (only 10 allowed at a time) and executes a method. In...
126
6599
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.