启用计时器问题

时间:2012-03-19 12:27:09

标签: c#

我遇到了计时器的问题。默认情况下,计时器未启用,我有一个方法可以启动/启用计时器。

我尝试过使用:

timer1.Enabled = true;

timer1.Start();

两者都无济于事。

在调试期间,我已经验证了执行启用/启动命令,但计时器永远不会打勾。

先谢谢, 麦克

    private void timer1_Tick(object sender, EventArgs e)
    {

        if (CurState == HepsState.ReceivingFile)
        {
            int bytesLeft = (readFileSize - readByteCount);
            if ((bytesLeft <= 16) && (bytesLeft > 0))
            {
                if (timerValid == false)
                {
                    timerValid = true;
                }
                else
                {
                    int sendBytes = bytesLeft;
                    SendReceivedDataToFile(sendBytes);
                    readByteCount = readFileSize;
                    SetState(HepsState.Start, "");
                    dataReceived = false;                              //no more data in oldData[]
                    timerValid = false;
                    timer1.Enabled = false;
                }
            }
        }

        else if (CurState == HepsState.ReceivingFileInfo)
        {
            if (dataReceived == true)
            {
                if (timerValid == false)
                {
                    timerValid = true;
                }
                else
                {
                    CheckFileInfoPacket();
                    SetState(HepsState.Start, (DateTime.Now.ToString("HH:mm:ss")) + (":  File Check Complete.") + ("\n"));
                    dataReceived = false;                              //no more data in oldData[]
                    timerValid = false;

                    timer1.Enabled = false;
                }
            }
        }
    }

启用计时器的代码是:

    // looks at recieved data packet and verifies ID++ and when appropriate send previously recieved data (oldData) to file.
    private void CheckDataPacket(ref byte[] ReadBuffer)
    {
        byte[] Checksum = new byte[1];
        Checksum[0] = Convert.ToByte("00000000", 2);
        byte[] DataBytes = new byte[18];
        for (int i = 0; i < 17; i++)
        {
            DataBytes[i] = ReadBuffer[i + 2];
        }
        DataBytes[17] = Convert.ToByte("00000000", 2);


        if (ReadBuffer[1] == readByteID + 1)
        {
            int sendBytes = 16;
            SendReceivedDataToFile(sendBytes);
            //readByteCount = readByteCount + 16;

            for (int i = 0; i < 16; i++)                            // cganged from 17
            {
                oldData[i] = ReadBuffer[i + 3];
            }
            CalculateChecksum(DataBytes, ref Checksum);
            SendChecksumPacket(ref Checksum);
            readByteID++;
            int bytesLeft = (readFileSize - readByteCount);
            if ((bytesLeft <= 16) && (bytesLeft > 0))   // changed from 16 tp 32
            {
                timer1.Enabled = true;
                timer1.Start();
                //endTimerEnabled = true;
            }
        }
    }

在调试期间,断点是timer1.Enabled = true。

2 个答案:

答案 0 :(得分:0)

确保您的方法“timer1_Tick”附加到“timer1.Tick”事件。 您应该在* .cs或* .designer.cs文件中查找以下代码行:

timer1.Tick += new EventHandler(timer1_Tick);

答案 1 :(得分:-1)

您可能需要同时启用它并启动它,而不是其中一个。

timer.Enabled只告诉计时器它应该触发已经过去的事件。所以在你完成timer.Enabled = true之后你还需要调用timer.Start()

示例:

timer.Elapsed += OnTimedEvent;
timer.Enabled = true;
timer.Start();