2013-11-21 34 views
0

我真的很痛苦通過WinRT的視窗:存儲命名空間與所有它的asyncronousness。C++/CX WinRT的文件複製

我在我的頭文件中的下列私有成員:

//Members for copying the SQLite db file 
Platform::String^ m_dbName; 
Windows::Storage::StorageFolder^ m_localFolder; 
Windows::Storage::StorageFolder^ m_installFolder; 
Windows::Storage::StorageFile^ m_dbFile; 

而且我在實現文件下面的代碼塊:

//Make sure the SQLite Database is in ms-appdata:///local/ 
m_dbName = L"DynamicSimulations.db"; 
m_localFolder = ApplicationData::Current->LocalFolder; 
m_installFolder = Windows::ApplicationModel::Package::Current->InstalledLocation; 

auto getLocalFileOp = m_localFolder->GetFileAsync(m_dbName); 
getLocalFileOp->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>([this](IAsyncOperation<StorageFile^>^ operation, AsyncStatus status) 
{ 
    m_dbFile = operation->GetResults(); 

    if(m_dbFile == nullptr) 
    { 
     auto getInstalledFileOp = m_installFolder->GetFileAsync(m_dbName); 
     getInstalledFileOp->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>([this](IAsyncOperation<StorageFile^>^ operation, AsyncStatus status) 
     { 
      m_dbFile = operation->GetResults(); 
      m_dbFile->CopyAsync(m_localFolder, m_dbName); 
     }); 
    } 
}); 

我得到一個內存訪問衝突時,它得到到m_dbFile = operation->GetResults();

缺少什麼我在這裏?我來自一個C#背景中,這是很容易的東西做:/

我已經嘗試使用「於是」,而不是註冊的事件,但我甚至不能讓這些編譯。

謝謝你的幫助!

+2

我還沒有機會查看您的代碼,但作爲同步替代方案,可以考慮CopyFile2,它可以從Windows應用商店應用中調用。 –

+0

是的,這看起來好多了:) – Eric

+0

此鏈接解釋異步編程模型與C++:http://msdn.microsoft.com/en-us/library/windows/apps/hh780559.aspx –

回答

0

如果你有興趣在WinRT的解決方案,那就是:

似乎所有你想要做的就是將數據庫文件從安裝位置複製到本地文件夾。爲此,下面的代碼應該足夠了:

//Make sure the SQLite Database is in ms-appdata:///local/ 
m_dbName = L"DynamicSimulations.db"; 
m_localFolder = ApplicationData::Current->LocalFolder; 
m_installFolder = Windows::ApplicationModel::Package::Current->InstalledLocation; 

create_task(m_installFolder->GetFileAsync(m_dbName)).then([this](StorageFile^ file) 
{ 
    create_task(file->CopyAsync(m_localFolder, m_dbName)).then([this](StorageFile^ copiedFile) 
    { 
     // do something with copiedFile 
    }); 
}); 
0

我以前試過這個東西。不要這樣做:

if(m_dbFile == nullptr) 

而是驗證「狀態」的值。

if(status == AsyncStatus::Error)