我正在構建儀表板樣式Windowsform,我有各種定時器來更新窗體上的控件,並且所有工作都很好,除了一個,我使用的唯一一個標籤控制作爲指標。爲Label.Text賦值最終導致StackOverflowException
我有各種System.Timers更新所有存儲在運行5分鐘左右的類的指標的數據,然後另一個計時器設置爲更新每秒運行的GUI。出於某種原因,這個代碼:
l_LastShipment.Text = GlobalStatic.fulfillmentInd.Caption;
的GUI更新最終在一個StackOverflowException錯誤,通常是幾個小時後內。 fulfillmentInd.Caption只是類中的一個字符串變量,並且在錯誤時包含正確的數據(通常是「0:01」或類似的東西)。
最初的GUI定時器是一個System.Timer,但害怕調用更新標籤是錯誤來自,所以切換GUI計時器到Windows.Forms.Timer,所以它不需要調用,但錯誤仍然存在。
沒有遞歸發生,我甚至監視了更新GUI功能運行所花費的時間,即使發生錯誤時,它也總是小於1/10秒。
這裏是修剪GUI刷新功能:
private void guiHandleTimer(object sender, EventArgs e)
{
//Refresh the Indicators
//Stopwatch
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//Compact indicator
if (GlobalStatic.compactInd.Changed())
{
pb_Compact.Value = GlobalStatic.compactInd.value;
pb_Compact.Caption = GlobalStatic.compactInd.Caption;
pb_CompactTT.SetToolTip(pb_Compact, GlobalStatic.compactInd.tooltip);
GlobalStatic.compactInd.Reset();
}
//
//Other Indicators removed for readability
//
//Fulfillment Indicator
if (GlobalStatic.fulfillmentInd.Changed())
{
if (GlobalStatic.fulfillmentInd.value <= 59)
{
//Within an Hour, Light Yellow
p_fulfillment.BackColor = Color.LightYellow;
}
else if (GlobalStatic.fulfillmentInd.value <= 300)
{
//Between an hour and 5 hours, Change to Green
p_fulfillment.BackColor = Color.Green;
}
else
{
//Over 5 hours old, Grey
p_fulfillment.BackColor = Color.LightGray;
}
l_LastShipment.Text = GlobalStatic.fulfillmentInd.Caption; <-------This is the line that generates the StackOverflow ----->
ToolTip test = new ToolTip();
test.SetToolTip(p_fulfillment, GlobalStatic.fulfillmentInd.tooltip);
test.SetToolTip(l_LastShipment, GlobalStatic.fulfillmentInd.tooltip);
GlobalStatic.fulfillmentInd.Reset();
}
stopwatch.Stop();
TimeSpan t = stopwatch.Elapsed;
if(TimeSpan.Compare(largestTime,t)==-1)
{
//largestTime is shorter than t, change largestTime
largestTime = t;
}
T_GUI.Text = largestTime.ToString(@"hh\:mm\:ss\:fff");
}
這裏是定時器設置,窗體加載過程中調用,即GUIHandleTimer在代碼中提到的唯一的地方:
//GUI Refresh Timer
System.Windows.Forms.Timer guiTimer = new System.Windows.Forms.Timer();
guiTimer.Interval = 1000;
guiTimer.Tick += new EventHandler(guiHandleTimer);