抱歉混淆標題,但老實說我不知道??該放什么。所以我創建了一個計時器,它會在每個 T 間隔觸發一個事件。這個簡單的事件只會附加文本并以 MMORPG 風格顯示它。它確實有效,但有一點很奇怪,它有時不會觸發事件(我解釋得對嗎?)。
這是圖片,所以你可以理解我的意思:

這是我的代碼:
baloon = new System.Timers.Timer(30);
baloon.Elapsed = OnTweakTimedEvent_baloon;
//baloon.Elapsed = new ElapsedEventHandler(OnTweakTimedEvent_baloon);
private void OnTweakTimedEvent_baloon(object sender, ElapsedEventArgs e)
{
lbBaloon.Text = message[letterCount];
letterCount ;
if (letterCount > message.Length - 1) //stops timer when finishes
{
letterCount = 0;
baloon.Enabled = false;
lbBaloon.Text = message; // displays the full message when it ends
}
}
uj5u.com熱心網友回復:
一個相當明顯的問題是您使用的System.Timers.Timer是 UI,沒有任何同步。如果沒有SynchronizationContext,此計時器將在后臺執行緒上引發事件,這可能會導致各種問題,因為 UI 類只能由 UI 執行緒安全訪問。
我建議使用 SynchronizationContext 或切換到 winforms 計時器。有關更多詳細資訊,請參閱計時器比較。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/332905.html
