發(fā)現(xiàn)blend4的一個(gè)導(dǎo)致崩潰的BUG!!!
正在用VS2010 + BLEND4做東東,因?yàn)槲沂荲S上寫(xiě)代碼,BLEND上搞界面,在VS2010寫(xiě)完代碼后,轉(zhuǎn)到BLEND上突然崩潰了,重新打開(kāi)BLEND4,甚至重啟系統(tǒng)都不行。
錯(cuò)誤頁(yè)面:
錯(cuò)誤輸出
Microsoft? Expression? crash report:
Date: 2010-08-24 17-16-43
User: Thinkpad@THINKPAD-PC
Application Version: 4.0.1000.1000
Operating System Version: Microsoft Windows NT 6.1.7600.0
Common Language Runtime Version: 4.0.30319.1
System.UnauthorizedAccessException: Invalid cross-thread access.
在 MS.Internal.XcpImports.CheckThread()
在 MS.Internal.XcpImports.CreateObjectByTypeIndex(UInt32 typeIndex)
在 System.Windows.Threading.DispatcherTimer..ctor(UInt32 nativeTypeIndex)
在 System.Windows.Threading.DispatcherTimer..ctor()
在 System.Windows.Threading.Dispatcher..ctor()
在 System.Windows.Threading.Dispatcher.get_MainDispatcher()
在 System.Windows.DependencyObject.get_Dispatcher()
在 Vega.Toolbar.<.ctor>b__0(Object obj) 位置 ………………………….xaml.cs:行號(hào) 21
在 System.Threading._TimerCallback.TimerCallback_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
在 System.Threading._TimerCallback.PerformTimerCallback(Object state)
由于在VS2010上沒(méi)有問(wèn)題,并且能夠編譯通過(guò)運(yùn)行良好,所以是認(rèn)定是Blend的問(wèn)題,然后我刪掉一些新加的代碼,Blend就不崩潰了,最后我確認(rèn)了發(fā)生問(wèn)題的代碼
難道是用我用了新潮的語(yǔ)法?我改成這樣再試
依然崩潰,再試:
obj => { this.Dispatcher.BeginInvoke(() => this.txtTime.Text = DateTime.Now.ToLongTimeString()); }
), null, 0, 1000);
再再試:
{ this.Dispatcher.BeginInvoke(() => this.txtTime.Text = DateTime.Now.ToLongTimeString()); }
), null, 0, 1000);
我快崩潰了,再再再試:
{
this.Dispatcher.BeginInvoke(() => this.txtTime.Text = DateTime.Now.ToLongTimeString());
}), null, 0, 1000);
再再再再試
{
this.Dispatcher.BeginInvoke(delegate()
{
this.txtTime.Text = DateTime.Now.ToLongTimeString();
});
}), null, 0, 1000);
到這,我確定了,和lambda表達(dá)式?jīng)]有關(guān)系,和匿名委托也應(yīng)該沒(méi)有關(guān)系,我還發(fā)現(xiàn)了一個(gè)細(xì)節(jié),我注銷(xiāo)掉這行代碼也是沒(méi)有問(wèn)題的,但是恢復(fù)代碼,一生成項(xiàng)目就崩潰,那么再再再再再試
new Timer(obj => this.Dispatcher.BeginInvoke(() => this.txtTime.Text = DateTime.Now.ToLongTimeString()), null, 1000, 1000);
這么改就好了,我想Blend是要把編譯的dll預(yù)運(yùn)行一下,但是如果Timer立即執(zhí)行,Blend還沒(méi)準(zhǔn)備好,出現(xiàn)了多線程的異常。

浙公網(wǎng)安備 33010602011771號(hào)