C#timer - 获取'Method name expected'错误

时间:2010-12-07 12:11:01

标签: c# syntax-error

当我尝试设置我的计时器的tick事件并使用该方法时,我收到此错误。这里出了什么问题?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;

namespace QueueSimulation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {  
        }

        public void goButton_Click(object sender, EventArgs e)
        {
            ProcessCustomers CustomerQueue = new ProcessCustomers(); // create the CustomerQueue

            System.Windows.Forms.Timer queueTimer = new System.Windows.Forms.Timer();
            queueTimer.Interval = Convert.ToInt32(customerArriveChooser.Value*1000);   
            queueTimer.Tick += new ElapsedEventHandler(CustomerQueue.Arrive()); 

            CustomerQueue.Arrive();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            // put code here to break out of the program
        }
    }

    public class Customer
    {
        int timeInQueue;
    }

    public class ProcessCustomers
    {   
        public void Arrive(){}

        public void Leave(){}
    }

    public class Server
    {
        bool servingStatus = false; // true for serving, false for not serving
    }

    public class Queue
    {
        Customer[] queue = new Customer[49]; // initialise a queue (array) capable of holding 50 customers
    }
}

2 个答案:

答案 0 :(得分:1)

我怀疑你的意思是使用方法 name ,而不是调用它并使用返回值:

queueTimer.Tick += new EventHandler(CustomerQueue.Arrive);

由于Arrive的返回值不是委托类型,因此无法使用它。

请注意,事件处理程序签名应与委托签名匹配 - 如果是Tick,则为EventHandler

public delegate void EventHandler(
    Object sender,
    EventArgs e
)

因此,您的Arrive方法应该采用以下两个参数:

public void Arrive(Object sender, EventArgs e){}

答案 1 :(得分:0)

ElapsedEventHandlerSystem.Timer不是System.Windows.Forms.Timer

的句柄

事件必须如下所示:

queueTimer.Tick += new ElapsedEventHandler(queueTimer_Tick); 


void queueTimer_Tick(object sender, EventArgs e)
{
    CustomerQueue.Arrive();
}