My clients run the same method which does something while(true) in the task? The method uses a static list. They take an element from list by your id, they never use the same element. And after it deletes this element from the list. I can have a lot of clients I don't know how much and I can't do my list for everyone. I cant send element to queue, because when one client lost connection would block other clients. Lock() doesn't work in Task.
This work for this moment, but throw exception.
Do you know any good way to solve my problem?
Task.Run(() =>
{
var element = list.First(x => x.Id == client.Id);
}
Code from my Project:
Task.Run(async() =>
{
await semaphore.WaitAsync();
try
{
foreach (var s in dataToSend)
{
data.Add(new DataForLists() { Id = s.Key, Model = s.Value });
}
}
finally
{
semaphore.Release();
}
});
Second Method
Task.Run(async () =>
{
while (true)
{
try
{
if (Compute.data.Count > 0)
{
if (Compute.data.Any(x => x.Id == DictionaryStreamProvider.streamsDictTcpSend[stream]))
{
await Compute.semaphore.WaitAsync();
try
{
var t = Compute.data.First(x => x.Id == DictionaryStreamProvider.streamsDictTcpSend[stream]);
Compute.data.Remove(t);
formatter.Serialize(stream, t.Model);
Compute.data.Remove(t);
Console.WriteLine("Wyslano {0} na liste: {1}", t.Model.Count, data2);
}
finally
{
Compute.semaphore.Release();
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
});