読者です 読者をやめる 読者になる 読者になる

Haruyuki Mohri

毛利春幸のブログです。

How to use "async" and "future<>" in XE8(Win64)

Use the "async", how to get the http data asynchronously.

"C++Builder XE8" is only in Win64, are supported C++11.
So, "future<>" it can also be used.

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    String url = L"http://www.mojeld.com/";
    std::future<String> fs;
    fs = std::async(std::launch::async, [url]()->String {
        std::shared_ptr<TStringStream> ss(new TStringStream(url,TEncoding::UTF8, false));
        std::shared_ptr<TIdHTTP> http(new TIdHTTP(NULL));
        http->Get(url, ss.get());
        String stGet = ss->DataString;
        Sleep(1000);
        return stGet;
        });
// std::this_thread::sleep_for(std::chrono::milliseconds(100));
    fs.wait();
    Memo1->Lines->Append( fs.get() );
}

I used a lambda.
But, the method that does not use a lambda is also possible.
f:id:mojeld:20150809010644p:plain

C++Builder(Win32) also hope the C++11 support.