C#-当我使用task::wait()方法时,它会引发异常

C#-当我使用task::wait()方法时,它会引发异常,c#,windows-store-apps,task,wait,C#,Windows Store Apps,Task,Wait,我正在编程Windows应用商店应用程序,我有以下问题。 我使用此代码将数字数组记录到文件中: auto item = KnownFolders::PicturesLibrary; task<StorageFile^> getFileTask(item->CreateFileAsync(filename, CreationCollisionOption::ReplaceExisting)); getFileTask.then([=](StorageFile^ storageFi

我正在编程Windows应用商店应用程序,我有以下问题。 我使用此代码将数字数组记录到文件中:

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();

为什么呢?我正在使用VS2013。请帮帮我。

我找到了这个问题的解决方案!!! 右侧代码:

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;
});
无符号字符a[]={1,2,3};
自动arr=ref新数组(a,sizeof(a));
自动m_istream=ref new InMemoryRandomAccessStream();
自动输出=m_istream->GetOutputStreamAt(0);
自动数据仓库=参考新数据写入器(outs);
数据仓库->写字节(arr);
任务(dw->StoreAsync())。然后([=](无符号整数)
{
返回项->CreateFileAsync(文件名,CreationCollisionOption::ReplaceExisting);
})。然后([=](存储文件^StorageFile)
{
返回storageFile->OpenAsync(FileAccessMode::ReadWrite);
})。然后([=](irandomaccesstream^新的\u流)
{
返回RandomAccessStream::CopySync(m_istream->GetInputStreamAt(0),new_stream->GetOutputStreamAt(0));
})。然后([=](UINT64复制字节)
{
返回;
});

如果您找到了问题的解决方案,您应该将其作为答案发布,并给出解释。是的,我同意您的看法,但我仍然无法做到,因为我的声誉不到10。
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;
});