C# 计时器 - 获取“预期的方法名称”错误
当我尝试设置计时器的滴答事件并使用该方法时,出现此错误。这里出了什么问题?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑您的意思是使用方法 name,而不是调用它并使用返回值:
由于
Arrive
的返回值不是委托类型,因此您不能使用它。请注意,事件处理程序签名应与委托签名匹配 - 在
Tick
的情况下,它是EventHandler
:因此,您的
Arrive
方法应采用这两个参数:I suspect you mean to use the method name, not call it and use the return value:
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 isEventHandler
:So, your
Arrive
method should take these two parameters:ElapsedEventHandler
是System.Timer
的句柄,不是System.Windows.Forms.Timer
的句柄,事件必须如下所示:
ElapsedEventHandler
is the handle forSystem.Timer
not forSystem.Windows.Forms.Timer
the event must look like this: