mardi 22 mars 2016

c# SIngelton Pattern With new Task

I have listenerclass. which has methods listening msmque, and recieve prices, and distribute depends on some conditions. getting prices so fast, and performace is very important that point. When i reading code i realized that, each received item from msmque creates new Task to make some process and distribute price. Here is the thing when a price recieved, the new price comes before first price distribute. To solve that problem each price use another task, but "orderService.DistributePrice" coded as a singelton pattern, I can not be sure that, when i create new task, each task create their own singelton, or each new task uses the same singelton instance. if yes, code is completely fail. Please let me now. thank you

public class Listener
{
    private  void PriceListener(Object source, PeekCompletedEventArgs asyncResult)
    {
         var mq = (MessageQueue)source;
         var recvMessage = mq.Receive();
        if (recvMessage != null)
        {
            var price = JsonConvert.DeserializeObject<InstrumentPrice>((string)recvMessage.Body);
            Task.Factory.StartNew(() => orderService.DistributePrice(price))
        }
        mq.BeginPeek();
    }
}

Aucun commentaire:

Enregistrer un commentaire