首页 > 其他分享 >WPF中的Frozen(冻结)与线程及其他相关问题

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

时间:2023-02-28 11:01:00浏览次数:31  
标签:冻结 Freezable Frozen Media System Windows Animation 线程 WPF


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类的对象,才可以被冻结。

标签:冻结,Freezable,Frozen,Media,System,Windows,Animation,线程,WPF
From: https://blog.51cto.com/JohnsonJu/6090465

相关文章

  • 关于.Net中的计时器及WPF中最适合的计时器问题
    .Net中,至少可以找出5个计时器类型:(1)System.Threading.Timer(2)System.Timers.Timer(3)System.Windows.Forms.Timer(4)System.Web.UI.Timer(5)System.Windows.Threading.Dis......
  • java网络编程-线程池服务端
    上篇文章我们借助线程实现了服务端可以服务多个客户端,但是每次请求进来都创建新线程也是一种很大的资源消耗,线程上下文切换都会影响性能。本次我们继续对服务端进行改造,引......
  • WPF 精修篇 拖拽 DragDrop
    WPF实现拖拽效果<Grid><Grid.ColumnDefinitions><ColumnDefinitionWidth="197*"/><ColumnDefinitionWidth="209*"/><Colum......
  • WPF 精修篇 依赖属性
    依赖属性使用场景1.希望可在样式中设置属性。2.希望属性支持数据绑定。3.希望可使用动态资源引用设置属性。4.希望从元素树中的父元素自动继承属性值。5.希望属性可进......
  • WPF下字体模糊的问题
    一直以来,发现WPF中的小字体下的文字变得比较模糊,比如:WPF与Winform字体显示比较:为了看到更清楚,我们放大点显示: 放得更大些:中文、日文等亚洲文字的显示也存在着类似的问题:在......
  • WPF中的文字修饰——上划线,中划线,基线与下划线
    我们知道,文字的修饰包括:空心字、立体字、划线字、阴影字、加粗、倾斜等。这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线、中划线、基线与下划线。如图:从上至下......
  • 给WPF文字加多条修饰线
    这是上篇​​WPF中的文字修饰——上划线,中划线,基线与下划线​​效果图:XAML代码:<Pagexmlns="​​http://schemas.microsoft.com/winfx/2006/xaml/presentation​​​"xmlns......
  • WPF应用程序顶级标签一定是Window吗?
    WPF应用程序顶级标签一定是Window吗? 很多人误以为是。可是,答案却是否定的。我们不妨来测试一下。首先使用顶级标签为Window,这是最普通、也是最常见的情况。新建一个WPF应......
  • java使用nio多线程读文件
    模拟多线程nio读取文件,并输出,output方法自己补一下。ReadFile代码:publicclassReadFileextendsObservable{privateintbufSize=1024;//换行符privateb......
  • 距离北京奥运还有359天,发布WPF版本的北京2008标志(下)
    图片显示效果: XAML代码:<ViewboxWidth="463.548828"Height="370.816895"xmlns="​​​http://schemas.microsoft.com/winfx/2006/xaml/presentation​​​"xmlns:x=......