qui*_*yst 4 c# wpf wpf-controls
我在类上有一个依赖属性,我们称之为'SomethingControl':
public static readonly DependencyProperty SomethingProperty = DependencyProperty.Register("Something", typeof(Something), typeof(SomethingControl), new UIPropertyMetadata(SomethingGotSet));
Run Code Online (Sandbox Code Playgroud)
我已经定义了作为异步传递到UIPropertyMetadata构造函数的回调方法:
private async static void SomethingGotSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (SomethingControl)d;
if (control != null)
{
control.SomeOtherProperty = await AsynchronousMethodCall();
}
}
Run Code Online (Sandbox Code Playgroud)
这样做安全吗?这是否会导致UI线程出现死锁或问题?我已经测试了它似乎工作正常,但我在MSDN文档中找不到任何使用这样的异步属性回调方法.
从您向我们展示的内容来看,这看起来非常安全.
PropertyChangedCallback什么都不返回(无效).该框架将调用此作为"Fire-And-Forget"方法.
因此,您调用可能导致稍后回发到用户界面线程的事实很好,并且不会直接影响此更改通知流.
如果存在多个线程,并且一个线程正在等待另一个线程正在持有的资源,则会发生死锁,反之亦然.根据具体AsynchronousCall情况,系统中某处可能仍然存在死锁,但这不会是由于您的SomethingSet实施造成的.