课程名:Windows应用程序开发入门到精通六:如何使用.NET开发Windows应用程序

 

程序的托管执行

程序集

名称空间

委托

线程

应用程序域

特性

数据类型

反射

 

1,托管代码指第一次编译形成中间代码(MSIL),执行时需要再编译成本地代码(二进制代码).类加载器会加载中间语言代码,或dll中的类库,再调用JIT来编译为托管的本地代码,最后被托管地执行,在执行时会不断地进行安全性策略检查。在加载dll类库时,不是全部装载,而只是装载Main()方法所在的class,在执行时会检查所要调用的类是否已经在内存中,若没有则实时装载进来。




2006101705.jpg 


   2,IDisposable
接口 定义一种释放分配的非托管资源的方法。当托管对象不再使用时,垃圾回收器会自动释放分配给该对象的内存,不过,进行垃圾回收的时间不可预知。另外,垃圾回收器对窗口句柄、打开的文件和流等非托管资源一无所知。将此接口的 Dispose 方法与垃圾回收器一起使用来显式释放非托管资源。当不再需要对象时,对象的使用者可以调用此方法

None.gifusing System;
None.gif
using System.ComponentModel;
None.gif
None.gif
// The following example demonstrates how to create
None.gif
// a resource class that implements the IDisposable interface
None.gif
// and the IDisposable.Dispose method.
None.gif

None.gif
public class DisposeExample
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif    
//实现IDisposable的基类,通过实现此接口,就表明此类型的实例会分配非托管资源
InBlock.gif
    public class MyResource: IDisposable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
InBlock.gif        
private IntPtr handle;//指向一个内部非托管资源的指针
InBlock.gif
        
InBlock.gif        
private Component component = new Component();//类中使用的其他托管资源
InBlock.gif
    
InBlock.gif        
private bool disposed = false;//跟踪Dispose是否已经被调用
InBlock.gif

InBlock.gif    
InBlock.gif        
public MyResource(IntPtr handle)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.handle = handle;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
//实现IDisposable接口,不要让此方法virtual。派生类应该不允许override这个方法
InBlock.gif
        public void Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Dispose(
true);
InBlock.gif            
//此对象将被Dispose方法清理掉,因此,你需要调用GC.SuppressFinalize方法来
InBlock.gif            
//将此对象从需要终止的对象队列中移除,并且防止这个对象执行两次终止代码
InBlock.gif
            GC.SuppressFinalize(this);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
//若disposing为true,则方法被用户代码直接或间接调用。托管资源和非
InBlock.gif        
//托管资源可以被释放。
InBlock.gif        
//若disposing为false,则方法被运行时从解析器内部调用并且你不应该再应用其他对象,
InBlock.gif        
//只有非托管资源能被释放。
InBlock.gif
        private void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Check to see if Dispose has already been called.
InBlock.gif
            if(!this.disposed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{//检查Dispose是否已经被调用过了
InBlock.gif
            
InBlock.gif                
if(disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{//若disposing为true,释放所有托管和非托管资源
InBlock.gif

InBlock.gif                    component.Dispose();    
// 在此处释放托管资源
InBlock.gif

ExpandedSubBlockEnd.gif                }

InBlock.gif             
InBlock.gif                
// 在这调用适当的方法来清理非托管资源,
InBlock.gif                
// 若disposing为false,则只有下面的代码会执行 
InBlock.gif
                CloseHandle(handle);
InBlock.gif                handle 
= IntPtr.Zero;            
ExpandedSubBlockEnd.gif            }

InBlock.gif            disposed 
= true;         
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Use interop to call the method necessary  
InBlock.gif        
// to clean up the unmanaged resource.
InBlock.gif
        [System.Runtime.InteropServices.DllImport("Kernel32")]
InBlock.gif        
private extern static Boolean CloseHandle(IntPtr handle);
InBlock.gif
InBlock.gif        
//这个解析器只在Dispose方法没有被调用时才运行,它为你的基类提供了解析的
InBlock.gif        
//机会。在其派生类中不要提供解析器。
InBlock.gif
        ~MyResource()      
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Dispose(
false);//解析器内部调用,只释放非托管资源,托管资源由垃圾回收器负责
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Insert code here to create
InBlock.gif        
// and use the MyResource object.   
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}

None.gif
None.gif

3,COM Marshaller用于与COM进行互操作,CLR会把 COM组件包装成一个应用程序集,在此应用程序集中会把方法中使用的参数类型都映射为CLR的数据类型,CLR就会和COM Marshaller进行交互,而后者与真正的COM组件进行交互。

   

4.NET Framework 允许您异步调用任何方法。定义与您需要调用的方法具有相同签名的委托;公共语言运行库将自动为该委托定义具有适当签名的 BeginInvoke EndInvoke 方法。


   BeginInvoke
方法用于启动异步调用。它与您需要异步执行的方法具有相同的参数,只不过还有两个额外的参数。BeginInvoke 立即返回,不等待异步调用完成。BeginInvoke 返回 IasyncResult,可用于监视调用进度。EndInvoke 方法用于检索异步调用结果。调用 BeginInvoke 后可随时调用 EndInvoke 方法;如果异步调用未完成,EndInvoke 将一直阻塞到异步调用完成。EndInvoke 的参数包括您需要异步执行的方法的 out ref 参数以及由 BeginInvoke 返回的 IAsyncResult

 

调用了 BeginInvoke 后,可以:

1,进行某些操作,然后调用 EndInvoke 一直阻塞到调用完成。

2,使用 IAsyncResult.AsyncWaitHandle 获取 WaitHandle,使用它的 WaitOne 方法将执行一直阻塞到发出WaitHandle 信号,然后调用 EndInvoke

3,轮询由 BeginInvoke 返回的 IAsyncResult,确定异步调用何时完成,然后调用 EndInvoke

4,将用于回调方法的委托传递给 BeginInvoke。该方法在异步调用完成后在 ThreadPool 线程上执行,它可以调用 EndInvoke

None.gif    public delegate bool SimpleDelegate(string Message);
None.gif
None.gif    
public class DelegateExample
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public DelegateExample()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void CallMeBack(SimpleDelegate CallBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IAsyncResult result 
= CallBack.BeginInvoke("Calling you back from thread"+System.AppDomain.GetCurrentThreadId(),null,null);
InBlock.gif
InBlock.gif            
bool callBackResult = (bool)CallBack.EndInvoke(result);
InBlock.gif            System.Windows.Forms.MessageBox.Show(callBackResult.ToString(),
"Callback Result");
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
public class Test
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Delegates();
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
public static bool SimpleDelegateHandler(string Message)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Windows.Forms.MessageBox.Show(Message,
InBlock.gif                
"Code Running On Thread ID"+System.AppDomain.GetCurrentThreadId().ToString());
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static void Delegates()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DelegateExample ex 
= new DelegateExample();
InBlock.gif            ex.CallMeBack(
new SimpleDelegate(SimpleDelegateHandler));
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif

5,线程应用示例:

None.gif        public delegate void DoneDelegate(bool arg);
None.gif
None.gif        
private void DelegateHandler(bool Stopped)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            btnStart.Text 
= "Save";
InBlock.gif
InBlock.gif            
if(Stopped)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"Save Stopped","Stopped",
InBlock.gif                    MessageBoxButtons.OK,MessageBoxIcon.Warning);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"Save Done");
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif        
private void ProcessSave()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
bool Stopped = false;
InBlock.gif            
for(int i=1;i<=50;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Threading.Thread.Sleep(
100);
InBlock.gif                
if(btnStart.Text == "Stopped")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{//用户要求停止
InBlock.gif
                    Stopped = true;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
new DoneDelegate(DelegateHandler).BeginInvoke(Stopped,null,null);//开始异步回调
ExpandedBlockEnd.gif
        }

None.gif
None.gif
None.gif        
private void btnStart_Click(object sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if(btnStart.Text=="start")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                btnStart.Text 
= "Stop";
InBlock.gif                System.Threading.Thread t 
= new Thread(new System.Threading.ThreadStart
InBlock.gif                    (ProcessSave));
InBlock.gif                t.Start();
//启动新线程来处理
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                btnStart.Text 
= "Stopped";
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif

通过delegate可以把界面元素和处理代码隔离开,并且后者可以去访问界面元素(尽管两者不是同一个类下的成员)

6,思考题:

None.gif[assembly:CLSCompliant(true)]
None.gif
//[CLSCompliant(false)] 
None.gif
    public class Class2
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
public static void Main(String[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            UInt32 a1 
= GetMinValue();
InBlock.gif            UInt32 a2 
= GETMINVALUE();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static UInt32 GetMinValue()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return UInt32.MinValue;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static UInt32 GETMINVALUE()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return UInt32.MinValue;
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif

如果编译上面这段代码,会有什么问题?会报两种错误,因为[assembly:CLSCompliant(true)]指定了要遵从CLS性,因此由于 CLS 中没有指定 UInt32 类型,所以返回类型会报错,而且两个方法的签名除了大小写以外是一样的,因此也不符合CLS,(例如在VB.net里就区分不出大小写的差别,尽管在C#是可以区分的。)

 

当然,若把去掉[CLSCompliant(false)]的注释,就指明了此类是不需要符合CLS的,因此就不会报错了。