High frequency event handling in VB.NET

Posted on 2009-02-09
Last Modified: 2012-05-06
I'm working with a market data feed API in VB.NET.  API is written is written in C#.

This API calls a method in one of my classes to signal when an event such as a price tick or a trade arrives on a TCP socket.  This can happen upwards of 10,000 times per second.  After the event comes in, the data is converted from a byte stream into data structures and the data structures are used in a pricing model.

Each event can take a couple ms to process, so multi-threading is probably necessary.  If events are not processed in time, they queue up, but this is extremely undesirable, so we want to process events without them backing up.

I wanted to gather your thoughts on the do's and don't of processing this many events.  For example, should I use the ThreadPool.QueueWorkItem or should I build my own threadpool?

Has anyone used ThreadPool.QueueWorkItem for so many small work items?

If I build my own threadpool, the approach that makes sense is to create a wrapper for the thread, and suspend the thread.  When an event arrives, put the data into the wrapper and call resume() to make the thread process the event.  Once the processing is complete, do another suspend() because once a thread dies, it cannot be restarted.

If I build my own threadpool, what is the overhead of doing suspend/resume so frequently?  What have been your experiences with having many threads going on and off at the same time?

Thanks in advance
Question by:sevzas
    LVL 2

    Accepted Solution

    Suspend/Resume is deprecated. If you want your thread to wait, try using Threading.EventWaitHandle or something similar.

    If you keep the thread running all the time (Which is the whole point of using EventWaitHandle), there is very little overhead, probably not noticeable.

    As a queue, you could simply use System.Collections.Queue.
    At my Q6600 CPU, it could dequeue about 10 million items in 120ms, which apparently is more than you need, so it should work.

    In this code, they will however queue up, and you mentioned this was undesireable, so i dont know if this code is goign to help you, but here goes :)

    Make one class of this, and use Add(obj) to enqueue an item.
    Public Class TCPQueue
        Dim WH As New Threading.EventWaitHandle(False, Threading.EventResetMode.ManualReset)
        Dim Q As New System.Collections.Queue
        Public Sub New()
            Dim T As New Threading.Thread(AddressOf Worker)
        End Sub
        Public Sub Add(ByVal obj As Object)
        End Sub
        Private Sub Worker()
                Do While Queue.Synchronized(Q).Count > 0
                    Dim obj As Object = Queue.Synchronized(Q).Dequeue
                    'Process here
        End Sub
    End Class

    Open in new window


    Author Comment

    vigylant: Thanks for the code.  It's nice that you showed me how to use a waithandle.  This is a very helpful class.

    I can probably deploy an array of these queues to process the events.

    I'm still curious whether it would be just as effective to throw all of these events into the ThreadGroup that the CLR makes available to me.  Does anyone have experience with ThreadGroup ?

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now