C#system.timers.timer奇怪的行为

时间:2017-07-28 01:13:37

标签: c# algorithm timer system.timers.timer

我正在尝试实施欺负协调员选举算法。在该算法中,协调器每10秒发送一次活动消息,并且所有进程等待至少14秒才能继续接收,如果他们在该时间内没有收到消息,他们将启动死区协调器选举。

问题是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

注意:我正在使用被动服务器来广播来自每个进程的消息

1 个答案:

答案 0 :(得分:0)

我不知道导致问题的原因,但我认为能够快速找出原因,如果你记录启动和停止所有方法并分析输出。

这有助于确定: 1.正如@Idle_Mind建议的那样,您正在添加越来越多的处理程序 2.执行每种方法所花费的时间都在增加 还有更多...

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