0

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.

  • It is not possible to start two timers at the same time, so they can work in the same time, but they do not fire at the same time. In what situation you have to influence the order of timers ? Code that depends on that should not exist. Simplest solution is to have only one timer. – Holger Dec 01 '19 at 17:15
  • Your operations are not thready safe. You need check locking [1](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement), [2](https://learn.microsoft.com/en-us/dotnet/standard/threading/managed-threading-best-practices) and concurrent collections [1](https://learn.microsoft.com/en-us/dotnet/standard/collections/thread-safe/) – Eldar Dec 01 '19 at 17:15
  • @Holger I wrote missing. The interval values of the timers is different. One is 300ms while the other is 500ms. So there are two timers. What should I do in this situation? I guess a single timer wouldn't be possible. – Enes Başpınar Dec 01 '19 at 17:30
  • @Eldar I use a single thread. Is it still necessary to lock it? – Enes Başpınar Dec 01 '19 at 17:30
  • If your are accessing through timer tick it is not single threaded. Timers create their on threads. – Eldar Dec 01 '19 at 17:39
  • The timers should not depend on each other - there is nothing to do. Or is it important to you, what happens at 1500ms ? If yes, use a 100ms timer, count how often it ticks/elapses, and if count % 3==0 you do the one thing, and if count % 5 == 0 you do the other thing, and suddenly on 15 both things are done. And you can define, if the 300ms or the 500ms action is done first. They would run completly synchronous to each other, cause it's only one timer. Why do you involve Threads ? there is no reasons for that. – Holger Dec 01 '19 at 17:40
  • @Eldar Ah okay. Obviously I dont know how to lock. I'ill learn out now. – Enes Başpınar Dec 01 '19 at 17:46
  • @Holger A great idea. I'il try it now. I just want to ask you something. It's not about which timer class I'm using, is it? – Enes Başpınar Dec 01 '19 at 17:48
  • I wasn't aware there are different Timer classes, but really there are: https://stackoverflow.com/questions/1416803/system-timers-timer-vs-system-threading-timer They have a difference in there thread safety. But decide yourself if you necessary want to run your Event-Handlers in Background threads. It's not a problem, but the order is incidental than. They will run one after the other. The tick event is queued, it will not interrupt any thread. – Holger Dec 01 '19 at 17:53
  • Thank you so much. You have been very helpful. Have a good day. – Enes Başpınar Dec 01 '19 at 17:59

0 Answers0