Dispatcher. Invoke method can take either a Delegate or Action instance as a parameter. But Dispatcher. BeginInvoke method doesn't accept an Action instance; it supports only Delegate .
It forces me to cast a lambda expression to Action in case of BeginInvoke. For example:
Dispatcher.Invoke(() => Title = "foo"); // success
// Dispatcher.BeginInvoke(() => Title = "foo"); // error
Dispatcher.BeginInvoke((Action)(() => Title = "foo")); // success
Is there any reason why Dispatcher.BeginInvoke method couldn't take an Action parameter?
It's a design decision. Since WPF came with .NET 3.0 and probably WPF was developed during some years, maybe delegates like Action
, Action<T>
... or Func<T>
... weren't added in early .NET 3.0 alphas so they left the code using their own delegate.
In the other hand, you argue you need to perform a cast while you should instantiate Action
instead:
// This is possible!
Delegate d1 = new Action(() => Console.WriteLine("hello world"));
Dispatcher.BeginInvoke(new Action(() => { /* ---do stuff--- */ }));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.