System.Windows.Forms.Application.DoEvents();
Thread.Sleep(50);
原理:主動處理訊息佇列,讓 UI 執行緒立即繪製更新,否則批次操作會等所有邏輯完成才一次性重繪,無法看到動畫過程。
private void PowerCutAnalyze(Visio.Application app)
{
#region 取得操作設備與驗證
Visio.Shape shapeSelected = null;
try
{
Visio.Window wndVisio = app.ActiveWindow;
if (wndVisio.Selection.Count == 1)
shapeSelected = wndVisio.Selection.get_Item16(1);
}
catch { }
if (!VisioUtility.HasShapeInWindow(VisWindow))
{
MessageUtil.ShowWarning("圖紙上沒有設備, 不能執行該操作");
return;
}
#endregion
app.UndoEnabled = false; // 關閉復原提升效能
List<string> list = new List<string>();
string message = "";
// 業務層執行拓撲分析
list = powerCutBLL.RunPowerCutAnalyzing(app, shapeSelected, ref message);
app.UndoEnabled = true;
if (message != "")
{
MessageUtil.ShowError(message);
return;
}
if (list.Count > 0)
{
AnalyzeShapeIdList.Clear();
foreach (string shapeStrID in list)
AnalyzeShapeIdList.Add(Convert.ToInt32(shapeStrID));
RunColorChanging(app); // 觸發動畫
}
else
{
MessageUtil.ShowWarning("請檢查線路是否連接正確。");
}
}
RunPowerCutAnalyzing 回傳拓撲順序 ID 列表AnalyzeShapeIdList 作為動畫引擎的輸入資料源UndoEnabled 避免大量操作產生龐大復原記錄