C# 计时器 - 获取“预期的方法名称”错误

发布于 2024-10-06 06:44:52 字数 1569 浏览 1 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没结果 2024-10-13 06:44:52

我怀疑您的意思是使用方法 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){}

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){}
錯遇了你 2024-10-13 06:44:52

ElapsedEventHandlerSystem.Timer 的句柄,不是 System.Windows.Forms.Timer 的句柄,

事件必须如下所示:

queueTimer.Tick += new ElapsedEventHandler(queueTimer_Tick); 


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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文