Category Archives: .NET CLR

SynchronizationContext Learning

http://www.codeproject.com/KB/threads/SynchronizationContext.aspx多线程编程最常见的场景就是在Thead1中执行Threa2中的代码,再说具体一点就是UI线程启动一个工作线程,工作线程修改UI来汇报自己工作的状态。对于这种场景,可以获取UI 线程的SynchronizationContext对象,把它传递给工作线程,工作线程再调用SychronizationContext.Send 或Post来执行UI线程中的代码。 常见用法为//Form 构造器中获取contextthis.context = SynchronizationContext.Current; Thread thread = new Thread(t =>{    Thread.Sleep(150);     this.context.Post    (        r =>        {            this.ChangeUI();        },        null    );});thread.Start(); 注意:不是所有线程都可以获得SynchronizationContext对象。只有UI线程中生成一个UI Control(一般就是Form)后,调用SynchronizationContext.Current获取SynchronizationContext。 SynchronizationContext.Current是一个static属性,其返回值不是AppDomain唯一的,而是Thread唯一的。

Posted in .NET CLR | Leave a comment

ref key causes a compiling error

private void ProcessList<T>(ref IList<T> list){ } private void ProcessComponent(ref IComponent a){} List<int> list = new List<int>();this.ProcessList(ref list);   //Error!! this.ProcessList(list);       //Work wellComponent c = new Component();this.ProcessComponent(ref c);    //Error!! The error message are:cannot convert from ‘ref System.Collections.Generic.List<int>’ to ‘ref System.Collections.Generic.IList<int>’cannot convert from … Continue reading

Posted in .NET CLR | Leave a comment

Timer vs DispatcherTimer

MS已经提供了好几个timer类:System.Windows.Forms.Timer 控件windows timer, 其历史可以追溯到vb 1.0, 主要是为了方便windows froms程序的编写. 有一个Interval属性 为使用UI线程进行事务处理的单线程环境设计,依赖于OS的timer message,精度较差, 注意:windows不会向消息队列中放入多个WM_TIMER消息,而是将多个WM_TIMER消息合并成 一个消息(和WM_PAINT类似),如果设定WM_TIMER消息的间隔为1秒,而消息处理函数的执行 时间超过1秒,在消息3处理函数的执行期间程序不会收到WM_TIMER消息. System.Timers.Timer 用于Server端的多线程环境,和 System.Windows.Forms.Timer的架构完全不同。System.Timers.Timer运行时所在的线程不同于UI线程,如果要访问UI对象,就需使用Invoke或BegingInvoke把操作传递给UI线程的Dispatcher。System.Windows.Threading.DispatcherTimer和UI线程的Dispatcher运行于同一线程。 System.Threading.Timer 在代码中使用,不依赖于os 的timer 使用 TimerCallback delegate 来指定需要执行的函数,这个函数将执行在线程池的线程中,而不是生成System.Threading.Timer的线程中. 在timer生成后,可以使用System.Threading.Timer.Change()来重新定义timer的等待时间和执行间隔时间. 注意:如果使用了System.Threading.Timer,就要保持对Timer的引用,否则,Timer将会被GC回收,使用Timer.Dispose()可以释放Timer所占用的资源. 由于callback函数由线程池中的线程执行,如果timer的interval值小于callback函数的 执行时间,callback函数会被多个线程执行.如果线程池中的线程被用光,callback函数 会排队等待,不能如期执行. WPF提供了新的timer:System.Windows.Threading.DispatcherTimer:DispatcherTimer timer = new DispatcherTimer();timer.Interval = new TimeSpan(0, 0, 1);timer.Tick += new … Continue reading

Posted in .NET CLR | Leave a comment

.NET 3.x 开发资源

101 Visual Basic and C# Code Samples .NET Framework 3.5 Common Namespaces and Types Poster Visual Studio 2008 and .NET Framework 3.5 Training Kit Visual C# 2005 Keyboard Shortcut Reference Poster .NET Framework 3.5 Enhancements Training Kit

Posted in .NET CLR | Leave a comment

ASP.NET 开发兵器谱

1. Framework Design: FxCop http://blogs.msdn.com/fxcop  Framework Design Studio http://code.msdn.microsoft.com/fds Dependency Management Tools: http://code.msdn.microsoft.com/fxarch A tool for visualizing dependencies  NDepend 2. ASP.NET 性能优化 Microsoft Network Monitor   参考How to capture network traffic with Network Monitor(http://support.microsoft.com/kb/148942) Visual Round Trip Analyzer ASP.NET ViewState Helper … Continue reading

Posted in .NET CLR | Leave a comment

C# 4.0 的新功能

Anders Hejlsberg在PDC 2008的演讲"The Future of C#"中描述了未来C#4.0的核心新功能:Dynamic 简单记录核心内容,为以后的学习提供方向. 1.Dynamic 语言和Static语言的比较 2. C# 4.0的核心功能 Dynamically Typed Objects Optional and Named Parameters Improved COM Interoperability Co- and Contra-variance 3.Variance in C# 4.0 对interface和delegate 类型的支持 Statically checked definition-site variance Value types are always invariantIEnumerable<int> … Continue reading

Posted in .NET CLR | Leave a comment

(转贴)解析.net中ref和out的实质(高手勿进)

代码 class RefOut{     static void Ref_Out(ref int a, out int b, int c)     {         a = 15;         b = 127;         c = 99;     }     static void Main()     {         int i1 = -1;                            //ref要有初始值         int i2;                                   //out不需要初始化         Ref_Out(ref i1, out i2, … Continue reading

Posted in .NET CLR | Leave a comment

.NET的继承机制(未完成)

读解析.net中继承的实质,复习<<CLR via C#>> 中相关内容,整理如下: 从高级语言的层面看,.NET的继承机制用到了virtual 关键字, override关键字, new 关键字,如何正确使用这些特性?何时使用override,何时使用new? 从高级语言编译器的角度看,使用了这些高级语言特性的代码对应了怎样的IL代码?用到了哪些IL指令? 从Runtime的角度来看,CLR对象布局如何?这些IL指令又是如何被JIT即时编译和执行的?对应什么汇编代码? 用于研究的代码为     class Person    {        public void InstanceMethod()        {            Console.WriteLine("Person::InstanceMethod().");        }        public virtual void VirtualMethod()        {            Console.WriteLine("Person::VirtualMethod().");        }    }    class Daughter : Person    {        public new void VirtualMethod()        {            … Continue reading

Posted in .NET CLR | Leave a comment

ASP.NET 程序中的OutOfMemory

参考1.Who is this OutOfMemory guy and why does he make my process crash when I have plenty of memory left?http://blogs.msdn.com/tess/archive/2005/11/25/who-is-this-outofmemory-guy-and-why-does-he-make-my-process-crash-when-i-have-plenty-of-memory-left.aspx 2.Programming Applications for Microsoft Windows 4ed  Parg3 Memory Management 3.Microsoft Windows Internal 4e 对于一个32进程,32位的地址值可以用来访问4GB的空间(0x00000000–0xFFFFFFFF),低位的2GB空间(0x00000000–0x7FFFFFFF)为私有的地址空间,高位的2GB是OS的保留空间.对于Windows 2000 Advanced ServerWindows 2000 Datacenter Server, … Continue reading

Posted in .NET CLR | 2 Comments

VS 2005中跟踪查看.NET源码

通过Codeplex上的.NET Mass Downloader下载源码,然后配置VS 2005,就可以象VS2008那样跟踪.NET 源码了。详细信息见John Robbins’ Blog:

Posted in .NET CLR | Leave a comment