发现blend4的一个导致崩溃的BUG!!!

正在用VS2010 + BLEND4做东东,因为我是VS上写代码,BLEND上搞界面,在VS2010写完代码后,转到BLEND上突然崩溃了,重新打开BLEND4,甚至重启系统都不行。

 

错误页面:

 

错误输出 

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:行号 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上没有问题,并且能够编译通过运行良好,所以是认定是Blend的问题,然后我删掉一些新加的代码,Blend就不崩溃了,最后我确认了发生问题的代码

 

new Timer(obj => this.Dispatcher.BeginInvoke(() => this.txtTime.Text = DateTime.Now.ToLongTimeString()), null01000);

 

 

难道是用我用了新潮的语法?我改成这样再试

new Timer(obj => { this.Dispatcher.BeginInvoke(() => this.txtTime.Text = DateTime.Now.ToLongTimeString()); }, null01000);

 

依然崩溃,再试:

new Timer(new TimerCallback(
    obj 
=> { this.Dispatcher.BeginInvoke(() => this.txtTime.Text = DateTime.Now.ToLongTimeString()); }
), 
null01000);

 

再再试:

 

new Timer(new TimerCallback(delegate(object obj)
    { 
this.Dispatcher.BeginInvoke(() => this.txtTime.Text = DateTime.Now.ToLongTimeString()); }
), 
null01000);

 

 

 我快崩溃了,再再再试:

new Timer(new TimerCallback(delegate(object obj)
{
    
this.Dispatcher.BeginInvoke(() => this.txtTime.Text = DateTime.Now.ToLongTimeString());
}), 
null01000);

 

再再再再试

new Timer(new TimerCallback(delegate(object obj)
{
    
this.Dispatcher.BeginInvoke(delegate() 
    {
        
this.txtTime.Text = DateTime.Now.ToLongTimeString(); 
    });
}), 
null01000);

 

 到这,我确定了,和lambda表达式没有关系,和匿名委托也应该没有关系,我还发现了一个细节,我注销掉这行代码也是没有问题的,但是恢复代码,一生成项目就崩溃,那么再再再再再试

 new Timer(obj => this.Dispatcher.BeginInvoke(() => this.txtTime.Text = DateTime.Now.ToLongTimeString()), null10001000);

 

这么改就好了,我想Blend是要把编译的dll预运行一下,但是如果Timer立即执行,Blend还没准备好,出现了多线程的异常。 

 

 

posted @ 2010-08-24 17:46  subwayline13  阅读(2522)  评论(12编辑  收藏  举报