I have developed a chronometer to practice a little WCF
. In this case, the server is responsible for making a decrease of x
to 0
and the client receives in real time the state of the latter.
The problem I have is that if I execute several clients at the same time, the chronometer multiplies the decrement by 1 for each new running instance.
Could you explain to me the best way to deal with different instances, I have to perform some control on the clients that consume my service so that they do not collapse the server by calling all the same function at the same time?
By the way I do not know if I raised it well but client has a timer that every second calls a function (server) that makes the descent of the timer, could you explain the best way to do the countdown taking into account of not overloading the server and being able to manage many instances simultaneously?
Some simple example would go very well to clarify me more.
Class Service
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,
ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service : IService
{
public static int contador = 10;
public int DecrementoContador()
{
if (contador > 0)
{
return contador--;
}
else if(contador == 0)
{
contador = 10;
return 10;
}
else
{
return 0;
}
}
}
}
IService Interface
[ServiceContract]
public interface IService
{
[OperationContract]
int DecrementoContador();
}
}
Timer on client
private async void tm_contador_Tick(object sender, EventArgs e)
{
MyServices.ServiceClient client = new MyServices.ServiceClient();
var contador = await client.DecrementoContadorAsync();
if (contador > 0)
{
contador--;
lb_cronometro.Text = contador.ToString();
}
else
{
tm_contador.Stop();
}
}