gpt4 book ai didi

c# 禁用事件一段时间

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:05 25 4
gpt4 key购买 nike

我有这组函数可以在一段时间内注销事件 listBox1_SelectedValueChanged。

函数调用:Pause()。

有没有什么方法可以将此作为一个函数来执行,可以对各种事件执行相同的操作,例如:

暂停(listBox1.SelectedValueChanged)

暂停(按钮1.点击)

等等?

    private System.Windows.Forms.Timer disableEvent = new System.Windows.Forms.Timer();
private void Pause(int forTime = 200)
{
listBox1.SelectedValueChanged -= new EventHandler(listBox1_SelectedValueChanged);

disableEvent.Tick += new EventHandler(disableEvent_Tick);
disableEvent.Interval = (forTime);
disableEvent.Enabled = true;
disableEvent.Start();
}
private void disableEvent_Tick(object sender, EventArgs e)
{
if (disableEvent.Enabled == true)
{
disableEvent.Tick -= new EventHandler(disableEvent_Tick);
disableEvent.Stop();
disableEvent.Enabled = false;

listBox1.SelectedValueChanged += new EventHandler(listBox1_SelectedValueChanged);
}
}

最佳答案

我会为此使用 DateTime 字段。即使允许运行,我也会检查 SelectedValuedChanged()(不要注销事件)

例如:(伪)

public class Class1
{
private DateTime _isEnabledAfter = DateTime.MinValue;

public Class()
{
listBox1.SelectedValueChanged += new EventHandler(listBox1_SelectedValueChanged);
}

public void Pause(int timeMS)
{
// set the _isEnabledAfter in the future.
_isEnabledAfter = DateTime.Now.AddMilliseconds(timeMS);
}


public void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
// is it after _isEnabledAfter?
if(DateTime.Now < _isEnabledAfter)
// nope... do nothing.
return;

// do your thing.
}

}

它将为您节省一些时间和复杂性。


可能是这样的:

public class Class1
{
private TimeEnabledEvent _selectedValueChanged = new TimeEnabledEvent();

public Class1()
{
listBox1.SelectedValueChanged += (s, e) =>
{
if (_selectedValueChanged.IsEnabled)
listBox1_SelectedValueChanged(s, e);
};


_selectedValueChanged.Pause(200);
}


public void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
// do your thing.
}

}

public class TimeEnabledEvent
{
private DateTime _isEnabledAfter = DateTime.MinValue;

public void Pause(int timeMS)
{
// set the _isEnabledAfter in the future.
_isEnabledAfter = DateTime.Now.AddMilliseconds(timeMS);
}

public bool IsEnabled
{
get { return (DateTime.Now >= _isEnabledAfter); }
}
}

更新 2:

public partial class Form1 : Form
{
private TimeEnabledEvent _event = new TimeEnabledEvent();

public Form1()
{
InitializeComponent();
listBox1.SelectedValueChanged += _event.Check(ListBox1_SelectedValueChanged);
_event.Pause(1000);
}

private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
{
// do your thing
}
}

internal class TimeEnabledEvent
{
internal EventHandler Check(EventHandler listBox1_SelectedValueChanged)
{
return new EventHandler((ss, ee) =>
{
if (DateTime.Now >= _isEnabledAfter)
listBox1_SelectedValueChanged(ss, ee);
});
}

private DateTime _isEnabledAfter = DateTime.MinValue;

public void Pause(int timeMS)
{
// set the _isEnabledAfter in the future.
_isEnabledAfter = DateTime.Now.AddMilliseconds(timeMS);
}

}

关于c# 禁用事件一段时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40528189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com