Hi,
I am having difficulties and questions about the different methods to generate and get the results from threads. I am creating a Windows Store App via DirectX/XAML/C++
My task at hand is to generate some data and save it to a file, however the data can take minutes to generate. It looks like there are two options I can take to thread it, but I am unsure which is better or if one can't be used in the
scenario.
Option 1:
Using the Concurrency create_async and create_task functions, I have been able to successfully run the data generating code on a separate thread from the UI, but can't find a way to return the data from it. It sounds like I need to convert
the parameters to a HANDLE, but my attempt to do so was unsuccessful. Below is the code that works, with the my attempt to return a HANDLE commented out.
auto runMC = create_async([this]//(HANDLE vs)
{
m_main->GenerateMonteCarlo();//returns a std::vector<std::string>
});
create_task(runMC).then([this, file]//(HANDLE vs)
{
//std::vector<std::string>* vecStr = reinterpret_cast<std::vector<std::string>*>(vs);;
...
}
Option 2: (
http://msdn.microsoft.com/en-us/library/hh750082.aspx )
I have noticed that there may be a better way to generate threads and even show the progress of a thread, but am unsure if iit is worth using in this situation. I tried to change the GenerateMonteCarlo function above to be a task that
returns a std::vector<std::string> value, but had issues creating that function without 30-some errors cropping up.
I would prefer this option if it does allow the progress to be shown to the user.
Conclusion:
I am almost certain one of these is a viable option, but I could us some help selecting which one. Also, if there is a better way I will be glad to check it out.
Thanks for any help,
- Moe
(Full function to save the file. This does correctly generate and writes to a file)
void RadiationX::DirectXPage::MonteCarloButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
FileSavePicker^ saveFilePicker = ref new FileSavePicker();
saveFilePicker->SuggestedStartLocation = PickerLocationId::DocumentsLibrary;
saveFilePicker->DefaultFileExtension = ".txt";
saveFilePicker->SuggestedFileName = "MC.txt";
auto radxExtensions = ref new Platform::Collections::Vector<Platform::String^>();
radxExtensions->Append(".txt");
saveFilePicker->FileTypeChoices->Insert("Text", radxExtensions);
create_task(saveFilePicker->PickSaveFileAsync()).then([this](StorageFile^ file)
{
if (file != nullptr)
{
auto runMC = create_async([this]
{
m_main->GenerateMonteCarlo();//returns a std::vector<std::string>
});
create_task(runMC).then([this, file]
{
CachedFileManager::DeferUpdates(file);
Platform::Collections::Vector<String^>^ lines = ref new Platform::Collections::Vector<String^>();
lines->Append("RadiationX file -- XYcreations.com");
lines->Append("");
// write to file
create_task(FileIO::WriteLinesAsync(file, lines)).then([this, file]()
{
// Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
// Completing updates may require Windows to ask for user input.
create_task(CachedFileManager::CompleteUpdatesAsync(file)).then([this, file](FileUpdateStatus status)
{
...
});
});
});
}
});
}