關于DocumentCollection 的 DocumentActivationChanged 事件,查閱資料找到的解釋是:活動文檔視窗失活或銷毀時觸發。作為文檔級别的事件,我了解的是要切換或者打開關閉文檔才會觸發該事件,是以在做層表事件響應的時候訂閱了該事件,用以重新整理圖層清單。
後來發現:在進行一些圖元編輯操作(比如移動多段線節點啥)的時候,滑鼠會異常卡頓。最開始很納悶,以為是訂閱了 Object 相關事件造成的,專門控制變量做了好一陣測試,最後發現上述事件才是罪魁禍首,以下測試代碼:
[CommandMethod("TTT")]
public void Test()
{
Application.DocumentManager.DocumentActivationChanged +=
DocumentManager_DocumentActivationChanged;
}
private int count = 0;
private void DocumentManager_DocumentActivationChanged(object sender, DocumentActivationChangedEventArgs e)
{
count++;
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
"DocumentActivationChanged,Count: " + count + Environment.NewLine);
}
然後結果是這樣:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNiZpdmLyITNyMzNzYTM0ITMxkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.gif)
我他喵的,随便拖一個節點觸發幾百次,不卡才怪了。是以這個事件并不僅僅是文檔級别,應該是某些視窗重新整理都會觸發,慎用。
然後,順便測試了下文檔級别全部事件(13個)的觸發順序:
public void Test()
{
Application.DocumentManager.DocumentCreated += DocumentManager_DocumentCreated;
Application.DocumentManager.DocumentActivated += DocumentManager_DocumentActivated;
Application.DocumentManager.DocumentBecameCurrent += DocumentManager_DocumentBecameCurrent;
Application.DocumentManager.DocumentCreateStarted += DocumentManager_DocumentCreateStarted;
Application.DocumentManager.DocumentCreationCanceled += DocumentManager_DocumentCreationCanceled;
Application.DocumentManager.DocumentDestroyed += DocumentManager_DocumentDestroyed;
Application.DocumentManager.DocumentLockModeChangeVetoed += DocumentManager_DocumentLockModeChangeVetoed;
Application.DocumentManager.DocumentLockModeChanged += DocumentManager_DocumentLockModeChanged;
Application.DocumentManager.DocumentLockModeWillChange += DocumentManager_DocumentLockModeWillChange;
Application.DocumentManager.DocumentToBeActivated += DocumentManager_DocumentToBeActivated;
Application.DocumentManager.DocumentToBeDeactivated += DocumentManager_DocumentToBeDeactivated;
Application.DocumentManager.DocumentToBeDestroyed += DocumentManager_DocumentToBeDestroyed;
Application.DocumentManager.DocumentActivationChanged +=DocumentManager_DocumentActivationChanged;
}
private int step = 0;
private string fileName=@"d:test.txt";
private void DocumentManager_DocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
{
step++;
HtFileHelper.AppendText("step " + step + " :DocumentToBeDestroyed", fileName);
}
//後面就不一一列出....
文檔從打開到關閉,大緻順序如下:
順序 | 事件 |
---|---|
step 1 : | DocumentLockModeWillChange |
step 2 : | DocumentLockModeChanged |
。。。 | 。。。 |
step 9 : | DocumentActivationChanged |
。。。 | 。。。 |
step 26 : | DocumentCreateStarted |
。。。 | 。。。 |
step 29 : | DocumentToBeDeactivated |
。。。 | 。。。 |
step 34 : | DocumentActivationChanged |
step 35 : | DocumentCreated |
step 36 : | DocumentActivated |
step 37 : | DocumentBecameCurrent |
。。。 | 。。。 |
step 55 : | DocumentActivationChanged |
。。。 | 。。。 |
step 62 : | DocumentBecameCurrent |
。。。 | 。。。 |
step 65 : | DocumentToBeDestroyed |
step 66 : | DocumentDestroyed |
省略号是Step1+Step2組合的重複,意義不大,就删掉了,測試并不嚴謹,僅供參考。
最後,想起之前一個前輩的告誡:慎用事件,在你不确定的時候。