简体   繁体   中英

C# .net 4.5 code to .net 4.0

in visual studio 2012 .NET 4.5 i have this

 public async void Execute(object parameter)
    {
      task = action(parameter);
      OnCanExecuteChanged();
      await _task;
      OnCanExecuteChanged();
    }

can i have the same exact behavior in .NET 4.0 Visual studio 2010 without async and await and without hanging the UI?

Yes, you can use this code:

//To invoke the OnCanExecuteChanged in same thread that the Execute method
var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
_task.ContinueWith(t => OnCanExecuteChanged(), scheduler);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM