Re: [RESOLVED] WCF realtime
I have a question about the threadsafety of the _callbackList of the example.
On the MSDN site you can read the next about the List(T) Class:
Thread Safety
Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Next, I've set the maxConcurrentInstances to 10 in the app.config file
Code:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MetadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8001/BeerInventory/mex" />
<serviceThrottling
maxConcurrentCalls = "10"
maxConcurrentSessions = "10"
maxConcurrentInstances = "10"
/>
</behavior>
</serviceBehaviors>
</behaviors>
...
For updating the clients the next statement is used:
Code:
_callbackList.ForEach(
delegate(IBeerInventoryCallback callback) { callback.NotifyBeerInventoryChanged(guestName, -1); });
What happens when someone Leaves the party while the foreach loop is running. Normally you get an execption that the collection has changed. I can not really test it because the small number of clients that are connected.
To make a list thread safe the lock statement must be used.
So I tried the next
Code:
lock (_callbackList) {
Thread.Sleep(3500);
_callbackList.ForEach(
delegate(IBeerInventoryCallback callback) { callback.NotifyBeerInventoryChanged(guestName, -1); });
}
During the time of the sleep I click another client to leave the party (so the client is removed from the _callbackList). When the Thread.Sleep has finished, the client is allready been removed from the list although the list had been locked.
Does anybodu know how this exactly works?
Re: [RESOLVED] WCF realtime
Put some Debug.WriteLine statements in the related methods (and perhaps listing the ThreadIDs).
Then look at the debug spew in the Output window.
Sometimes the order that things occur may surprise you and the debug spew can be helpful in figuring this out.
Re: [RESOLVED] WCF realtime
Quote:
Originally Posted by dannystommen
During the time of the sleep I click another client to leave the party (so the client is removed from the _callbackList). When the Thread.Sleep has finished, the client is allready been removed from the list although the list had been locked.
Does anybodu know how this exactly works?
Yout must have
Code:
lock (_callbackList) {
Around the Removal code also, any you must make sure that both are locking on the same object.