简体   繁体   中英

C# timer - Getting the 'Method name expected' error

When I try to set the tick event of my timer, and use the method, I get this error. What's going wrong here?

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
    }
}

I suspect you mean to use the method name , not call it and use the return value:

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

Since the return value of Arrive is not a delegate type, you can't use it.

Note that the event handler signature should match the delegate signature - in the case of Tick , it is EventHandler :

public delegate void EventHandler(
    Object sender,
    EventArgs e
)

So, your Arrive method should take these two parameters:

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

ElapsedEventHandler is the handle for System.Timer not for System.Windows.Forms.Timer

the event must look like this:

queueTimer.Tick += new ElapsedEventHandler(queueTimer_Tick); 


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM