我正在使用Windows應用商店(C++)應用程序。這是一種使用Web服務從數據庫中讀取數據的方法。將異步函數的完成鏈連接到另一個
task<std::wstring> Ternet::GetFromDB(cancellation_token cancellationToken)
{
uriString = ref new String(L"http://myHost:1234/RestServiceImpl.svc/attempt");
auto uri = ref new Windows::Foundation::Uri(Helpers::Trim(uriString));
cancellationTokenSource = cancellation_token_source();
return httpRequest.GetAsync(uri, cancellationTokenSource.get_token()).then([this](task<std::wstring> response)->std::wstring
{
try
{
Windows::UI::Popups::MessageDialog wMsg(ref new String(response.get().c_str()), "success");
wMsg.ShowAsync();
return response.get();
}
catch (const task_canceled&)
{
Windows::UI::Popups::MessageDialog wMsg("Couldn't load content. Check internet connectivity.", "Error");
wMsg.ShowAsync();
std::wstring abc;
return abc;
}
catch (Exception^ ex)
{
Windows::UI::Popups::MessageDialog wMsg("Couldn't load content. Check internet connectivity.", "Error");
wMsg.ShowAsync();
std::wstring abc;
return abc;
}
} , task_continuation_context::use_current());
}
我很困惑如何將接收到的數據返回給調用函數。每當我試圖從GetFromDB接收返回的數據()
ternet.GetFromDB(cancellationTokenSource.get_token()).then([this](task<std::wstring> response)
{
data = ref new String(response.get().c_str());
});
我得到一個COM異常:現在,我在我的數據類的這樣的構造調用這個函數。但是,這在一個晴朗的運行:
ternet.GetFromDB(cancellationTokenSource.get_token());
請建議鏈接GetFromDB完成對其他代碼的一種更好的方式。然後如何從GetFromDB()的try {}塊中獲取返回的值。請記住,我是一名非常新的異步編程學生。
同樣,實際的異常錯誤代碼會有幫助。 – 2013-04-04 12:26:39
您可以將.get()調用包裝在try {/*...*/} catch(Platform :: Exception^e)Platform :: String^s = e-> Message中; }'然後設置一個斷點並檢查「s」以獲取有關異常的其他信息。 – 2013-04-11 22:39:07