我编写Windows Store App,我遇到了以下问题。 我使用此代码将数字数组记录到文件中:
auto item = KnownFolders::PicturesLibrary;
task<StorageFile^> getFileTask(item->CreateFileAsync(filename, CreationCollisionOption::ReplaceExisting));
getFileTask.then([=](StorageFile^ storageFile)
{
return storageFile->OpenAsync(FileAccessMode::ReadWrite);
}).then([](IRandomAccessStream^ m_istream)mutable
{
unsigned char a[] = { 1, 2, 3 };
auto arr = ref new Array<unsigned char>(a, sizeof(a));
auto outs = m_istream->GetOutputStreamAt(0);
auto dw = ref new DataWriter(outs);
dw->WriteBytes(arr);
return dw->StoreAsync();
}).wait();
代码已成功编译,但它提供了一个错误:
MyTest.exe已触发断点。
它指向_REPORT_PPLTASK_UNOBSERVED_EXCEPTION();
行,ppltasks.h
。
如果我使用.then([](DataWriterStoreOperation^){})
代替.wait()
,我的应用程序不会使用此错误进行编译:
error C2338: incorrect parameter type for the callable object in 'then';
consider _ExpectedParameterType or task<_ExpectedParameterType> (see below).
为什么?我用的是VS2013。请帮帮我。
答案 0 :(得分:2)
我找到了解决这个问题的方法! 正确的代码:
unsigned char a[] = { 1, 2, 3 };
auto arr = ref new Array<unsigned char>(a, sizeof(a));
auto m_istream = ref new InMemoryRandomAccessStream();
auto outs = m_istream->GetOutputStreamAt(0);
auto dw = ref new DataWriter(outs);
dw->WriteBytes(arr);
task<unsigned int>(dw->StoreAsync()).then([=](unsigned int)
{
return item->CreateFileAsync(filename, CreationCollisionOption::ReplaceExisting);
}).then([=](StorageFile^ storageFile)
{
return storageFile->OpenAsync(FileAccessMode::ReadWrite);
}).then([=](IRandomAccessStream^ new_stream)
{
return RandomAccessStream::CopyAsync(m_istream->GetInputStreamAt(0), new_stream->GetOutputStreamAt(0));
}).then([=](UINT64 copiedBytes)
{
return;
});