locked
C++ threading preference/abilities RRS feed

  • Question

  • 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)
    					{
    						...
    					});
    				});
    			});
    			
    		}
    	});
    }

    • Edited by C_Moe Thursday, June 5, 2014 3:10 AM
    Thursday, June 5, 2014 3:09 AM

Answers

  • Hi C_Moe - I've been looking at this question for several days and the best answer I can come up with for you is: test your options and decide which one is better for you.  As I look at the question, it occurs to me that you may not be able to use one or the other.  In that case, goes with the one that works. This is a pragmatic forum.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, June 9, 2014 1:00 PM
    Moderator