WPF中的Frozen(冻结)与线程及其他相关问题

来源:互联网 发布:好看的网络剧推荐 编辑:程序博客网 时间:2024/05/20 19:33

System.Windows.Freezable类(在 WindowsBase.dll 中)定义一个对象,该对象具有可修改状态和只读(冻结)状态。派生自 Freezable 的类提供详细的更改通知,可以是不可变的,并且可以进行自我克隆。
C#: public abstract class Freezable : DependencyObject
Freezable 类提供特殊功能,以便在使用修改或复制开销很大的对象时帮助提高应用程序性能
比如,常见的Freezable对象Brush,Pen,Geometry,Transform,AnimationTimeline(事实上,它们都继承自System.Windows.Media.Animation.Animatable,而Animatable又继承自System.Windows.Freezable,见后面的继承层次结构)等。

派生自 Freezable 的类可以获得以下功能:
(1)特殊状态:只读(冻结)状态可写状态
(2)线程安全性:可以在线程之间共享冻结的 Freezable 对象
(3)详细的更改通知:与其他 DependencyObject 对象不同,Freezable 对象可在子属性值更改时提供更改通知
(4)便于克隆:Freezable 类已经实现了多种生成深层克隆的方法。

需要注意的是,如果以下任何一个对象相关条件为 true,则无法冻结 Freezable 对象:
(1)它有动画或数据绑定的属性
(2)它有由动态资源设置的属性
(3)它包含无法冻结的 Freezable 子对象
如果这些条件对于 Freezable 对象为 false,并且您不希望修改它,请将其冻结以提高性能。将对象冻结,可以提高效率,因为被冻结的对象不会被改变,因此它们不需要监控。

SolidColorBrush属于Freezable 对象类型,下例演示如何通过调用其 Freeze 方法使 Freezable 变为只读。
C#代码:
Button button = new Button();
SolidColorBrush solidColorBrush = new SolidColorBrush(Colors.Red);         
if (solidColorBrush.CanFreeze)
{
    // 使画刷不可更改(冻结)
    solidColorBrush.Freeze();
}
button.Background = solidColorBrush; 

上例需要注意一点,如果SolidColorBrush solidColorBrush = new SolidColorBrush(Colors.Red);   这一行改成:
SolidColorBrush solidColorBrush = Brushes.Red;  则solidColorBrush处于冻结状态。也就是说,不能再被改变。同理,从SystemColors返回的画刷对象也是冻结的。比如:
Brush brush = SystemColors.WindowBrush; 这时的brush不允许改变它。
但可以使用:Brush brush = new SystemColorBrush(SystemColors.WindowColor);  //这个brush可以改变

如上面代码所示,如果Freezable对象的CanFreeze属性是true,则可调用Freeze()方法来实现对象的冻结和不可变动。确定 Freezable 是否处于冻结状态:使用 Freezable 对象的 IsFrozen 属性来确定对象是否处于冻结状态。也就是说,如果某Freezable对象的IsFrozen属性如果变成true,则表示此对象已经被冻结。

没有办法将已冻结的对象解冻(unfreeze),但你可以使用 Clone() 方法来创建只读 Freezable对象 的可写副本,这个副本就是没有冻结的。
冻结的Freezable对象可以在不同的线程之间共享,但没有被冻结的Freezable对象则不行。
只有继承自Freezable类的对象,才可以被冻结。

 

继承层次结构
System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.DependencyObject
      System.Windows.Freezable
        System.Windows.Media.Animation.Animatable
          System.Windows.FreezableCollection<(Of <(T>)>)
          System.Windows.Media.Animation.Timeline
          System.Windows.Media.Animation.TimelineCollection
          System.Windows.Media.Brush
          System.Windows.Media.DashStyle
          System.Windows.Media.Drawing
          System.Windows.Media.DrawingCollection
          System.Windows.Media.Effects.BitmapEffect
          System.Windows.Media.Effects.BitmapEffectCollection
          System.Windows.Media.Effects.BitmapEffectInput
          System.Windows.Media.GeneralTransform
          System.Windows.Media.GeneralTransformCollection
          System.Windows.Media.Geometry
          System.Windows.Media.GeometryCollection
          System.Windows.Media.GradientStop
          System.Windows.Media.GradientStopCollection
          System.Windows.Media.GuidelineSet
          System.Windows.Media.ImageSource
          System.Windows.Media.Media3D.Camera
          System.Windows.Media.Media3D.Geometry3D
          System.Windows.Media.Media3D.Material
          System.Windows.Media.Media3D.MaterialCollection
          System.Windows.Media.Media3D.Model3D
          System.Windows.Media.Media3D.Model3DCollection
          System.Windows.Media.Media3D.Rotation3D
          System.Windows.Media.Media3D.Transform3D
          System.Windows.Media.Media3D.Transform3DCollection
          System.Windows.Media.MediaPlayer
          System.Windows.Media.PathFigure
          System.Windows.Media.PathFigureCollection
          System.Windows.Media.PathSegment
          System.Windows.Media.PathSegmentCollection
          System.Windows.Media.Pen
          System.Windows.Media.TextEffect
          System.Windows.Media.TextEffectCollection
          System.Windows.Media.TransformCollection
          System.Windows.TextDecoration
          System.Windows.TextDecorationCollection
        System.Windows.Media.Animation.BooleanKeyFrame
        System.Windows.Media.Animation.BooleanKeyFrameCollection
        System.Windows.Media.Animation.ByteKeyFrame
        System.Windows.Media.Animation.ByteKeyFrameCollection
        System.Windows.Media.Animation.CharKeyFrame
        System.Windows.Media.Animation.CharKeyFrameCollection
        System.Windows.Media.Animation.ColorKeyFrame
        System.Windows.Media.Animation.ColorKeyFrameCollection
        System.Windows.Media.Animation.DecimalKeyFrame
        System.Windows.Media.Animation.DecimalKeyFrameCollection
        System.Windows.Media.Animation.DoubleKeyFrame
        System.Windows.Media.Animation.DoubleKeyFrameCollection
        System.Windows.Media.Animation.Int16KeyFrame
        System.Windows.Media.Animation.Int16KeyFrameCollection
        System.Windows.Media.Animation.Int32KeyFrame
        System.Windows.Media.Animation.Int32KeyFrameCollection
        System.Windows.Media.Animation.Int64KeyFrame
        System.Windows.Media.Animation.Int64KeyFrameCollection
        System.Windows.Media.Animation.KeySpline
        System.Windows.Media.Animation.MatrixKeyFrame
        System.Windows.Media.Animation.MatrixKeyFrameCollection
        System.Windows.Media.Animation.ObjectKeyFrame
        System.Windows.Media.Animation.ObjectKeyFrameCollection
        System.Windows.Media.Animation.Point3DKeyFrame
        System.Windows.Media.Animation.Point3DKeyFrameCollection
        System.Windows.Media.Animation.PointKeyFrame
        System.Windows.Media.Animation.PointKeyFrameCollection
        System.Windows.Media.Animation.QuaternionKeyFrame
        System.Windows.Media.Animation.QuaternionKeyFrameCollection
        System.Windows.Media.Animation.RectKeyFrame
        System.Windows.Media.Animation.RectKeyFrameCollection
        System.Windows.Media.Animation.Rotation3DKeyFrame
        System.Windows.Media.Animation.Rotation3DKeyFrameCollection
        System.Windows.Media.Animation.SingleKeyFrame
        System.Windows.Media.Animation.SingleKeyFrameCollection
        System.Windows.Media.Animation.SizeKeyFrame
        System.Windows.Media.Animation.SizeKeyFrameCollection
        System.Windows.Media.Animation.StringKeyFrame
        System.Windows.Media.Animation.StringKeyFrameCollection
        System.Windows.Media.Animation.ThicknessKeyFrame
        System.Windows.Media.Animation.ThicknessKeyFrameCollection
        System.Windows.Media.Animation.Vector3DKeyFrame
        System.Windows.Media.Animation.Vector3DKeyFrameCollection
        System.Windows.Media.Animation.VectorKeyFrame
        System.Windows.Media.Animation.VectorKeyFrameCollection
        System.Windows.Media.DoubleCollection
        System.Windows.Media.ImageMetadata
        System.Windows.Media.Int32Collection
        System.Windows.Media.Media3D.Point3DCollection
        System.Windows.Media.Media3D.Vector3DCollection
        System.Windows.Media.PointCollection
        System.Windows.Media.VectorCollection

当 IsFrozen 属性为 false 时,只能从创建 Freezable 对象的线程访问此对象。如果尝试从其他线程访问,则会引发 InvalidOperationException。Dispatcher.Invoke 和 Dispatcher.BeginInvoke 方法提供封送处理到正确线程的支持。
当它们的 IsFrozen 属性为 true 时,Freezable 对象为自由线程对象。

注意结构体(structure)不存在冻结不冻结的问题。比如:Color。说白了,它并不是继承自Freezable类的对象,而只有继承自Freezable类的对象,才可以被冻结。