我現在有個需求是在Timer的Tick觸發時傳遞引數:
基本上是這樣
t.Tick += (sender,args)=> t_Tick(sender,args,i,end);
public static void t_Tick(object o, EventArgs e,
Neo.ApplicationFramework.Tools.OpcClient.GlobalDataItem i, int end)
{
imgChange(i,end);
}
public static void imgChange(Neo.ApplicationFramework.Tools.OpcClient.GlobalDataItem i, int end)
{
int src = i.Value;
src = src < end ? src + 1 : 1;
i.Value = src;
}
但這事件怎么關閉啊?
public static void timerOff(Neo.ApplicationFramework.Tools.OpcClient.GlobalDataItem i, int end)
{
t.Tick -= (sender,args)=> t_Tick(sender,args,i,end);
t.Enabled = false;
}
這樣會報錯未應用到實體?什么原因?
uj5u.com熱心網友回復:
因為你使用了匿名函式的關系。把方法指定到event,然后直接+-event就可以了。
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
public EventHandler eve;
private void Form1_Load(object sender, EventArgs e)
{
eve = new System.EventHandler((aa, ee) => Tick(aa, ee, "111"));
t.Interval = 3000;
t.Tick += eve;
t.Start(); //3秒彈出一次 111
}
private void Tick(object sender, EventArgs e, string str)
{
MessageBox.Show(str);
}
private async void button1_Click(object sender, EventArgs e)
{
t.Stop();
t.Tick -= eve;
t.Start(); //查看是否還會彈出111
}
uj5u.com熱心網友回復:
因為兩次的匿名函式,雖然長得一模一楊,但是實際參考是不一樣的。就像
model m1=new model();
m1=new model();
這兩個new model(); 是不同的物件。
uj5u.com熱心網友回復:
Timer 已經是過去試了,趕緊丟了吧,使用 FluentScheduler它不香嗎
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/229610.html
標籤:C#
