Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a message queue that I need to parse. Some messages are urgent and need to be processed first.

I also need to use threading. Processing these messages in a linear fashion is far too slow.

At peak times I could have a backlog of thousands.

I tried using a ThreadPool. The speed was significantly improved but I could not (or don't know how to) process priority messages first.

I tried using a Dispatcher, but none of the threads actually run:
C#
public delegate void NextPrimeDelegate();
private Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;

public void ParseMessageQueue()
{

    //Projections message is a db reference to the instance.
    WorkflowDataAccess.Projections.Message message;

    //Get next message from the queue.  If the queue is empty then it will wait this thread until one arrives.  All these messages have 'read'=true
    while ((message = _messageQueue.Next) != null)
    {
    //Debug message
    Logs.Log(string.Format(@"3. Message Id: {0}", message.Id));

        //Get a loop safe version of m
        var m = message;
        //Queue the method and item

        var next = new NextPrimeDelegate(() => { CallBack(m); });
        DispatcherPriority dispatcherPriority;
        switch (m.Urgency)
        {
            case 0:
                dispatcherPriority=DispatcherPriority.Normal;
                break;
            default:
                dispatcherPriority = DispatcherPriority.Send;
                break;
        }

        //Callback does not get run >_<
        _dispatcher.BeginInvoke(dispatcherPriority, next);
    
        //threads without priority
        //ThreadPool.QueueUserWorkItem(CallBack, m,);

        //Linear
        //CallBack(m);
    }
}


Maybe I'm doing it wrong?

Any suggestions how I can improve the performance and prioritize the urgent messages?

Thanks ^_^
Andy
Posted
Updated 1-Jul-15 23:33pm
v2

1 solution

A good reference point[^] on not touching priority of Thread Pool

You might find this link[^] interesting
 
Share this answer
 
Comments
Andy Lanng 2-Jul-15 6:11am    
Hi Asif. Thanks for the response.
I wouldn't mess with ThreadPool priority. That's why I'm trying to find an alternative.

I looked at SmartThreadPool but I seemed like overkill. I might try it :/
Andy Lanng 2-Jul-15 6:44am    
Ok - I used SmartThreadPool with two main groups - One for normal priority and another for highest. I think that is the best I can get without suspending the normal group for every urgent message that comes in.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900