- There has a Collection variable and an Add method at my WCF servcie side. The Add method would log some records to the Collection while something trigger . Here are the requirements: Now at the client Side, there has a Thread to read the Collection at WCF service side every second and remove the recorde read before and display them to the foreground.
My question is : How to Control the conccur of reading and writing would be better.( is not thread safe, so there will get duplicate values while reading it)
Currently I use a Timer to concurrency it, when the timer event is triggered, make the Timer stop untill the reading ended. And then let the Timer start(to prevent the timer Event not executed completely , triggered the next Operation again). I use Monitor.TryEnter as thread lock, and the code is like the following:
void ReadFromService()
{
//do........
timer.start();//
}
Timer timer=new Timer(1000);
timer.Elapsed +=(s,e)=>{
timer.stop();
ReadFromService();
};
timer.start();
I think this is not a good idea, I hope someone can give me something good suggestion.