Hi everyone, <div>I have found another problem relating to this. I am simply asking the provider for market data and saving them to database. I implemented Grant's solution in following way: <br><br><i><font color="#006600">// this method is running in the working thread, which is saving the data to DB</font></i><br>
<div><i style="background-color:rgb(255,255,255)"><font color="#3333ff">void WorkerMethod() </font></i></div><div><i style="background-color:rgb(255,255,255)"><font color="#3333ff">{</font></i></div><div><div><i style="background-color:rgb(255,255,255)"><font color="#3333ff"> while (true) </font><font color="#006600">// infinite loop is checking the queque and sending the top pair to DB</font></i></div>
<div><i style="background-color:rgb(255,255,255)"><font color="#3333ff"> {</font></i></div><div><i style="background-color:rgb(255,255,255)"><font color="#3333ff"> if (!CurrencyPairQueue.IsEmpty())</font></i></div><div>
<i style="background-color:rgb(255,255,255)"><font color="#3333ff"> {</font></i></div><div><i style="background-color:rgb(255,255,255)"><font color="#3333ff"> ServiceDatabase.SaveToDB(CurrencyPairQueue.GetTopPair());</font></i></div>
</div><div><div><i style="background-color:rgb(255,255,255)"><font color="#3333ff"> System.Threading.Thread.Sleep(25); // sleep the thread for 25ms for lower CPU load</font></i></div></div><div><div><i style="background-color:rgb(255,255,255)"><font color="#3333ff"> }</font></i></div>
</div><div><i style="background-color:rgb(255,255,255)"><font color="#3333ff"> }</font></i></div><div><i style="background-color:rgb(255,255,255)"><font color="#3333ff">}</font></i></div><div><br></div><div>The queque is saving the data as it goes from the OnMessage event in main thread, if some currency pair come more then once, the older data is dropped. </div>
<div><br></div><div>The issue in my solution is high CPU load (cca 15% in average with 25ms sleep) of my service. I thought of several possibilities to fix this: </div><div><br></div><div>1) Get rid of the infinite loop. I thought about this, but I did not find a better way how to do it since the queque is almost never empty (any event-like solution will be very similar to this in my opinion). </div>
<div>2) Save whole queque in one insert statement. The problem is that the pairs are saving to database through storage procedure (it also process the data), so the entire process would have to be rewriten. And I am not entirely convinced it would help. </div>
<div><br></div><div>I also tried to play with the Thread.Sleep value - the higher values is forcing too much values to drop out from the queque. </div><div><br></div><div>What solution do you think will be the best? Or maybe I am all wrong and you can find another one. </div>
<div><br></div><div>Regards, </div><div>kwitee</div><div><br></div></div>