这是NumericUpDown
ValueChanged
事件的代码:为什么在numericUpDown changed事件中调用函数时会让一切都变慢?
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
DoThresholdCheck();
}
这是DoThresholdCheck()
代码:
private void DoThresholdCheck()
{
List<int> f;
List<string> fts;
const string D6 = "000{0}.bmp";
if (Directory.Exists(subDirectoryName))
{
if (!File.Exists(subDirectoryName + "\\" + averagesListTextFile + ".txt"))
{
return;
}
else
{
f = new List<int>();
fts = new List<string>();
Bitmap myFiles;
int counter = 0;
double thershold = (double)numericUpDown1.Value;
double max_min_threshold = (thershold/100) * (max - min) + min;
_fi = new DirectoryInfo(subDirectoryName).GetFiles("*.bmp");
for (int i = 0; i < myNumbers.Count; i++)
{
if (myNumbers[i] >= max_min_threshold)
{
string t = i.ToString("D6") + ".bmp";
if (File.Exists(subDirectoryName + "\\" + t))
{
button1.Enabled = false;
myTrackPanelss1.trackBar1.Enabled = true;
}
else
{
button1.Enabled = true;
myTrackPanelss1.trackBar1.Enabled = false;
pictureBox1.Image = Properties.Resources.Weather_Michmoret;
label5.Visible = true;
break;
}
}
}
if (myTrackPanelss1.trackBar1.Maximum > 0)
{
SetPicture(0);
myTrackPanelss1.trackBar1.Scroll += new EventHandler(trackBar1_Scroll);
}
if (_fi.Length >= 0)
{
label15.Text = _fi.Length.ToString();
label15.Visible = true;
}
}
}
else
{
button1.Enabled = true;
}
}
我想要做的是;每次我更改程序中的NumericUpDown
值时,它都会调用该事件中的函数。如果我每次创建一个新的临时列表并执行操作并对该列表进行一些检查。
问题是当我在程序中上下数次更改NumericUpDown
值,然后尝试使用trackBar1
再次在图像之间移动时,trackbar1
移动得非常缓慢。最初运行程序时,trackbar1
中的滑动条快速移动并在图像间快速移动,但当更改NumericUpDown
值时,由于某种原因,轨迹栏缓慢移动。
我试着添加一个Timer2
,也许在NumericUpDown
中用它来使它在500ms后才能调用函数,但是它没有解决它。
我不明白为什么它的移动太慢了。
斯莱德我所做的是在调用函数之前,在数字下降更改事件我刚刚添加myTrackPanelss1.trackBar1.Scroll - = new EventHandler(trackBar1_Scroll);取消注册事件处理程序。我不知道它是一个很好的解决方案,但它的工作。 –
您是否需要添加和删除该事件处理程序?你能不能在类的构造函数中添加事件处理程序,然后在销毁类时删除它?这将是控制事件处理的更好方法。除非您以目前的方式添加和删除它的具体原因。 –