這是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);取消註冊事件處理程序。我不知道它是一個很好的解決方案,但它的工作。 –
您是否需要添加和刪除該事件處理程序?你能不能在類的構造函數中添加事件處理程序,然後在銷燬類時刪除它?這將是控制事件處理的更好方法。除非您以目前的方式添加和刪除它的具體原因。 –