Không có sự khác biệt như là phương pháp BeginInvoke
gọi một LegacyBeginInvokeImpl
phương pháp riêng mà itslef gọi phương thức riêng InvokeAsyncImpl
(phương pháp được sử dụng bởi InvokeAsync
). Vì vậy, về cơ bản là điều tương tự. Nó có vẻ như nó là một tái cấu trúc đơn giản, tuy nhiên nó là lạ các phương pháp BeginInvoke
không được gắn cờ là lỗi thời.
BeginInvoke:
public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method)
{
return this.LegacyBeginInvokeImpl(priority, method, null, 0);
}
private DispatcherOperation LegacyBeginInvokeImpl(DispatcherPriority priority, Delegate method, object args, int numArgs)
{
Dispatcher.ValidatePriority(priority, "priority");
if (method == null)
{
throw new ArgumentNullException("method");
}
DispatcherOperation dispatcherOperation = new DispatcherOperation(this, method, priority, args, numArgs);
this.InvokeAsyncImpl(dispatcherOperation, CancellationToken.None);
return dispatcherOperation;
}
InvokeAsync:
public DispatcherOperation InvokeAsync(Action callback, DispatcherPriority priority)
{
return this.InvokeAsync(callback, priority, CancellationToken.None);
}
public DispatcherOperation InvokeAsync(Action callback, DispatcherPriority priority, CancellationToken cancellationToken)
{
if (callback == null)
{
throw new ArgumentNullException("callback");
}
Dispatcher.ValidatePriority(priority, "priority");
DispatcherOperation dispatcherOperation = new DispatcherOperation(this, priority, callback);
this.InvokeAsyncImpl(dispatcherOperation, cancellationToken);
return dispatcherOperation;
}
Hiện tại tôi đang nhận được các ngoại lệ chưa được xử lý hoạt động như mong đợi bằng BeginInvoke (kích hoạt cả DispatcherUnh andledException trên dispatcher và AppDomain.CurrentDomain.UnhandledException), nhưng các ngoại lệ chưa được giải quyết trên InvokeAsync đang bị âm thầm nuốt chửng. Tiếp tục nhiệm vụ từ InvokeAsync với một cái gì đó để bẫy ngoại lệ có vẻ là một công việc hợp lệ xung quanh. – Lamarth
'BeginInvoke' được tạo khuôn mẫu sau [" Mô hình lập trình không đồng bộ "] (https://msdn.microsoft.com/en-us/library/ms228963 (v = vs.110) .aspx) trong .NET sử dụng' BeginSomething Phương thức 'và' EndSomething' cho các phép toán không đồng bộ. Có lẽ đây là lý do tại sao nó không được chỉ định không được chấp nhận hoặc lỗi thời. Ngoại trừ quy ước 'Begin' /' End' là sử dụng 'IAsyncResult', và' BeginInvoke' không, cũng không có bất kỳ 'EndInvoke' nào, vì vậy nó là loại thừa định ngay từ đầu. – sidbushes