locked
create file issue

    Question

  • hi all,

    i want to create a file in metro app, i test 3 ways below:

    void test1()
    {
    	try
    	{
    		Platform::String^ strPath = ref new Platform::String(TEXT("result2.txt"));
    		auto op = Windows::Storage::ApplicationData::Current->LocalFolder->CreateFileAsync(strPath, Windows::Storage::CreationCollisionOption::ReplaceExisting);
    		op->GetResults();
    	}
    	catch(Platform::Exception^ msg)
    	{}
    }
    
    void test2()
    {
    	try
    	{
    		Platform::String^ strPath = ref new Platform::String(TEXT("result2.txt"));
    		auto op = Windows::Storage::ApplicationData::Current->LocalFolder->CreateFileAsync(strPath, Windows::Storage::CreationCollisionOption::ReplaceExisting);
    		auto tsk = create_task(op);
    		tsk.get();
    	}
    	catch(Platform::Exception^ msg)
    	{}
    }
    
    void test3()
    {
    	try
    	{
    		Platform::String^ strPath = ref new Platform::String(TEXT("result2.txt"));
    		auto op = Windows::Storage::ApplicationData::Current->LocalFolder->CreateFileAsync(strPath, Windows::Storage::CreationCollisionOption::ReplaceExisting);
    		auto tsk = create_task(op);
    		tsk.then([=](Windows::Storage::StorageFile^ file)
    		{
    			int m;
    		});
    	}
    	catch(Platform::Exception^ msg)
    	{}
    }

    only test3 works, other two will crash, can anybody explain that.

    i also tried ofstream in iostream.h, it does not work neither.

    BTW:my goal is to create a file Synchronous, not async.
    • Edited by jffo_ye Monday, September 24, 2012 10:13 AM
    Monday, September 24, 2012 10:06 AM

Answers

  • Hi jffo_ye,

    I tried by your above code, test2 , test3 works and only test1 got exception.

    In test1, for op->GetResults() you got exception. I don't know why you use it. Actually GetResults() Returns the results of the action and it does not perform any action on your asynchronous operation.

    If you are executing your code from UI thread you can use task chain for maintaining sequence, because UI thread can not allow any blocking for synchronization.

    task chains, -> myTask.then(…).then(…).then(…);

    If you are executing your code from a separate worker thread you can use task like following for synchronization.

    Platform::String^ strPath = ref new Platform::String(TEXT("result2.txt"));
    auto op = Windows::Storage::ApplicationData::Current->LocalFolder->CreateFileAsync(strPath, Windows::Storage::CreationCollisionOption::ReplaceExisting);			
    create_task(  op ).then([this](StorageFile^ file)
        {
    	if(  nullptr != file )
                    {
    			currentFile  = file;
    		}
           
        }).then([] (task<void> previousTask) 
    	{
    		try
    			{
    				previousTask.get();
    				OutputDebugStringW(L"File created successfully : \n");
    			}
    			catch(Exception^ e)
    			{
    				OutputDebugStringW(L"Failed to create file \n");
    			}
    
    	}).wait();

    You can also use event inside try catch block for synchronization(How to open file in sync?).



    Monday, September 24, 2012 5:46 PM