第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何同步運(yùn)行異步任務(wù)<T>方法?

如何同步運(yùn)行異步任務(wù)<T>方法?

一只斗牛犬 2019-06-06 14:51:54
如何同步運(yùn)行異步任務(wù)<T>方法?我正在學(xué)習(xí)異步/等待,并遇到需要同步調(diào)用異步方法的情況。我怎么能這么做?異步方法:public async Task<Customers> GetCustomers(){     return await Service.GetCustomersAsync();}正常使用:public async void GetCustomers(){     customerList = await GetCustomers();}我嘗試使用以下方法:Task<Customer> task = GetCustomers();task.Wait()Task<Customer> task = GetCustomers();task.RunSynchronously(); Task<Customer> task = GetCustomers();while(task.Status != TaskStatus.RanToCompletion)我還從這里但是,當(dāng)調(diào)度程序處于掛起狀態(tài)時(shí),它無法工作。public static void WaitWithPumping(this Task task) {         if (task == null) throw new ArgumentNullException(“task”);         var nestedFrame = new DispatcherFrame();         task.ContinueWith(_ => nestedFrame.Continue = false);         Dispatcher.PushFrame(nestedFrame);         task.Wait();}下面是調(diào)用的異常和堆棧跟蹤RunSynchronously:System.InvalidOperationException訊息:對于未綁定到委托的任務(wù),可能不會同步調(diào)用。InnerException*空來源*姆斯科利布斯塔克跡:        at System.Threading.Tasks.Task.InternalRunSynchronously(TaskScheduler scheduler)    at System.Threading.Tasks.Task.RunSynchronously()    at MyApplication.CustomControls.Controls.MyCustomControl.CreateAvailablePanelList() in C:\Documents and Settings\..    .\MyApplication.CustomControls\Controls\MyCustomControl.xaml.cs:line 638    at MyApplication.CustomControls.Controls.MyCustomControl.get_AvailablePanels() in C:\Documents and Settings\.    ..\MyApplication.CustomControls\Controls\MyCustomControl.xaml.cs:line 233    at MyApplication.CustomControls.Controls.MyCustomControl.<CreateOpenPanelList>b__36(DesktopPanel panel) in C    :\Documents and Settings\...\MyApplication.CustomControls\Controls\MyCustomControl.xaml.cs:line 597    at System.Collections.Generic.List`1.ForEach(Action`1 action)    at MyApplication.CustomControls.Controls.MyCustomControl.<CreateOpenPanelList>d__3b.MoveNext() in C:\Documents and Settings    \...\MyApplication.CustomControls\Controls\MyCustomControl.xaml.cs:line 625
查看完整描述

3 回答

?
阿晨1998

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個贊

建議這個答案已經(jīng)三年了。我編寫它的大部分是基于.NET 4.0的體驗(yàn),很少使用4.5,特別是async-await..一般來說,這是一個很好的簡單的解決方案,但它有時(shí)會破壞一些東西。請閱讀評論中的討論。

.凈4.5

就用這個:

// For Task<T>: will block until the task is completed...var result = task.Result; 
// For Task (not Task<T>): will block until the task is completed...task2.RunSynchronously();

見:任務(wù)服務(wù)生任務(wù)結(jié)果任務(wù)同步運(yùn)行


.net 4.0

用這個:

var x = (IAsyncResult)task;task.Start();x.AsyncWaitHandle.WaitOne();

.或者這個

task.Start();task.Wait();


查看完整回答
反對 回復(fù) 2019-06-06
  • 3 回答
  • 0 關(guān)注
  • 921 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號