2017-07-28 162 views
0

我想实施欺负协调员选举算法。在该算法中,协调器每10秒发送一次活动消息,所有进程至少等待14秒才能接收活动消息,如果他们在那段时间内没有收到消息,他们将启动死选协调器选举。C#system.timers.timer奇怪的行为

问题是AliveTimer(Timer3_Count)正在呈指数级增长,活动进程也在影响它。我不知道它为什么表现怪异。

当初始协调者发送Alive消息时,计数器工作正常,但是在死选协调员选举后,其行为异常。

else if (Received_Text.Contains("Alive:")) 
      { 
       SetText(Received_Text + "\n"); 
       Coordinator_Alive = true; 

       Timer3_Counter = 0; 

       if (Alive_Count == 0) 
       { 
        Alive_Count++; 


        AliveTimer.Interval = (1 * 1000); 
        AliveTimer.Enabled = true; 
        AliveTimer.Elapsed += new System.Timers.ElapsedEventHandler(AliveTimer_Elapsed); 
        AliveTimer.Start(); 

       } 

      } 

逝去的功能在这里 我觉得有什么不对我的计划,我都试过了。

private void AliveTimer_Elapsed(object sender, EventArgs e) 
    { 
     Timer3_Counter++; 
     SetTimer(Timer3_Counter.ToString()); 

     Random rnd = new Random(); 
     int rand_time = rnd.Next(14, 18); 

     if (Timer3_Counter == 14) 
     { 
      AliveTimer.Stop(); 

      Timer3_Counter = 0; 
      Alive_Count = 0; 

      if (Coordinator_Alive == false) 
      { 
       byte[] buffer = Encoding.ASCII.GetBytes("Dead Coordinator Election: " + txName.Text); 
       _clientSocket.Send(buffer); 

       Timer4_Counter = 0; 

       DeadTimer.Interval = (1 * 1000); 
       DeadTimer.Elapsed += new System.Timers.ElapsedEventHandler(DeadTimer_Elapsed); 
       DeadTimer.Enabled = true; 
       DeadTimer.Start(); 

      } 

     } 

     if (Coordinator_Alive == true) 
      Coordinator_Alive = false; 

    } 

和死者协调员选码是这里

else if (Received_Text.Contains("Dead Coordinator Election:")) 
      { 
       SetCPID(""); 
       Coordinator_Alive = false; 
       Alive_Count = 0; 
       Timer3_Counter = 0; 

       AliveTimer.Stop(); 
       AliveTimer.Enabled = false; 


       string output = Regex.Match(Received_Text, @"\d+").Value; 
       SetText("Dead Coordinator Election Received from Process ID: " + output + "\n"); 

       if (Convert.ToInt32(txName.Text) > Convert.ToInt32(output)) 
       { 
        byte[] buffer = Encoding.ASCII.GetBytes("Greater Process No: " + txName.Text + " found than " + output + "\n"); 
        _clientSocket.Send(buffer); 
        SetText("Our Process No: " + txName.Text + " is Greater than " + output + "\n"); 
        Lower_Count++; 

        byte[] buffer1 = Encoding.ASCII.GetBytes("Dead Coordinator Election: " + txName.Text); 
        _clientSocket.Send(buffer1); 
       } 
       else 
       { 
        byte[] Txt_Send = Encoding.ASCII.GetBytes("Our Process No: " + txName.Text + " is less than " + output); 
        _clientSocket.Send(Txt_Send); 
        Greater_Count++; 
       } 

      } 

完整的代码可以在这里找到 Bully Algorithm

注:我使用的被动式服务器只是广播从每个进程

消息
+2

_问题是AliveTimer(Timer3_Count)正在呈指数级增长,并且活动进程也在影响它。我不知道它为什么表现怪异。你几乎肯定会多次连接'Elapsed()'处理程序。这会导致您的代码每次“tick”运行多次。你应该只在一次**之前连接那个处理程序**,通常在你创建它的时候。 –

+0

当我停止并重新启动计时器时,它开始多次滴答滴答。 如何避免这种情况? – Mayur

+0

...以及你如何停止并启动计时器?请告诉我们该代码。 –

回答

0

我不知道是什么原因引起的问题,但我认为将能够迅速找出原因,如果你日志启动和停止所有的所有方法和分析输出。

这将有助于确定是否: 1. @Idle_Mind建议,要添加更多处理器 2.执行每个方法小兵 多花费的时间...

我不不知道你的应用程序是如何构建的,但你甚至可以从Console.WriteLineDebug.WriteLine开始。