I have a problem. I'm doing a project using Thread and Timer classes. It doesn't start at the same time (I tried it when they were both 500ms). One will be 300ms and the other 500ms.
Thread:
Thread hostServerRequestThread= new Thread(() =>
{
this.BeginInvoke((Action)delegate ()
{
hostServerIncomingRequestTimer.Start();
hostServerOutgoingRequestTimer.Start();
});
});
hostServerRequestThread.Start();
Timer Ticks (I've assigned a number instead of random to try):
private void HostServerIncomingRequestTimer(object sender, EventArgs e)
{
Console.WriteLine("HostServerIncomingRequestTimer");
int random = 50;
servers[0].totalIncomingRequest += random;
servers[0].waitingRequest += random;
}
private void HostServerOutgoingRequestTimer(object sender, EventArgs e)
{
Console.WriteLine("HostServerOutgoingRequestTimer");
int random = 50;
if (servers[0].waitingRequest >= random)
{
sunucular[0].totalOutgoingRequest+= random;
sunucular[0].waitingRequest -= random;
}
else if (servers[0].waitingRequest < rastgeleSayi && servers[0].waitingRequest >= 0)
{
sunucular[0].totalOutgoingRequest+= sunucular[0].waitingRequest ;
sunucular[0].waitingRequest -= sunucular[0].waitingRequest ;
}
}
Second HostServerOutgoingRequestTimer_Tick:
private void HostServerOutgoingRequestTimer_Tick(object sender, EventArgs e)
{
Console.WriteLine("HostServerOutgoingRequestTimer");
int random = 50;
servers[0].totalOutgoingRequest += random;
servers[0].waitingRequest -= random;
}
First hostServerOutgoingRequestTimer is running. Then hostServerIncomingRequestTimer. Don't you think it's weird? Although I wrote hostServerIncomingRequestTimer.
Also If use second HostServerOutgoingRequestTimer_Tick, it works simultaneously without trouble. If use first HostServerOutgoingRequestTimer_Tick, it works like this (incoming-outcoming): 50-50, 100-100, 150-150. If use second HostServerOutgoingRequestTimer_Tick, it works like this (incoming-outcoming): 50-0, 100-50, 150-100. Does the If-Else affect this much? When I measure time, the time is almost the same on both timers.
I don't have any other threads or timers in my code. How do I determine which timer should work first? Or how can i do it another way? Thanks for your answers.