I have a self hosted wcf singleton service. There are two client consuming it. Service has one instance variable of type List. There are three methods. One to add, one to remove and last to check if list is empty. Client1 is using only add method. Client2 uses remove and isEmpty method. I am wondering is lock required in this case in these methods? Any better approach for this problem to enhance performance?
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service : IService
{
// will it be better to make it static ?
List<Parameter> _fileList = new List<Parameter>();
// Is this required ?
readonly object _lock = new object();
public bool FileEnQueue(Parameter parameter)
{
lock (_lock)
{
_fileList.Add(parameter);
}
return true;
}
public Parameter FileDeQueue()
{
lock (_lock)
{
Parameter parameter = new Parameter();
if (_fileList.Count > 0)
{
parameter = _fileList.ElementAt(0);
_fileList.Remove(parameter);
}
return parameter;
}
}
public bool IsQueueEmpty()
{
lock (_lock)
{
if (_fileList.Count == 0)
return true;
else
return false;
}
}
}