locked
when callback return

    Question

  • when call streamsocket connectaysnc()

    I want to make socket has connected

    but it happened error

    event_wait = OpenEventW(EVENT_ALL_ACCESS,false,smieventname);	
    task<void> connect(socket->ConnectAsync(ref new HostName(IP), IP_port, SocketProtectionLevel::PlainSocket));
        connect.then([=] (task<void> previousTask)
        {
            try
            {	
                previousTask.get();
                SetEvent(event_wait);			
            }
            catch (Exception^ exception)
            {         
            }			
        });
    WaitForSingleObjectEx(event_wait,INFINITE,false);	

    it will hang up WaitForSingleObjectEx()

    why ?

    I did a test

    I don't know when I modify the code like this

    event_wait = OpenEventW(EVENT_ALL_ACCESS,false,smieventname);
    task<void> connect(socket->ConnectAsync(ref new HostName(IP), IP_port, SocketProtectionLevel::PlainSocket));
        connect.then([=] (task<void> previousTask)
        {
            try
            {
                previousTask.get();
                SetEvent(event_wait);
            }
            catch (Exception^ exception)
            {        
            }
        });

    StorageFolder^ baseFolder = KnownFolders::DocumentsLibrary;
    baseFolder = Windows::Storage::ApplicationData::Current->LocalFolder;

    WaitForSingleObjectEx(event_wait,INFINITE,false);

    many times it will be OK 

    when "baseFolder = Windows::Storage::ApplicationData::Current->LocalFolder;" completed

    it will goto ::

    previousTask.get();

    SetEvent(event_wait);

    but it can't work  in other place



    • Edited by oishixixi Wednesday, March 14, 2012 8:32 AM
    Wednesday, March 14, 2012 8:01 AM

Answers

  • Hello,

    DataReaderLoadOperation is a async function. You should call DataReaderLoadOperation.GetResults() function, so that the function will wait until it get the result or been canceled. If you did not call this function, the task will be run, maybe after main function. Because them are in different thread.

    In addition, for the structured_task_group Class. We should add the tasks through structured_task_group::run, and then wait them in structured_task_group::wait Method. 

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by oishixixi Wednesday, March 28, 2012 9:20 AM
    Monday, March 26, 2012 6:57 AM
  • Dear Jesse

    thank  you very much.

    I found the reason that I can't use ReadBytes() after LoadAsync() completed.

    because I test it in UI main thread. if I new a thread to do this test ,Readbytes() will not go to error.

    thank you.

    • Marked as answer by Jesse Jiang Thursday, March 29, 2012 2:25 AM
    Wednesday, March 28, 2012 9:24 AM

All replies

  • add::

    if I use task to call a Asynchronous  operation .

    when the app is going ,and the task is running background ?

    when use socket connectaysnc ,if the connectaysnc hasn't completed ,the APP is going and try send data ,the app will failed.

    so why connect operation  is Asynchronous 

    Thursday, March 15, 2012 6:56 AM
  • Hello,

    As far as I know, the connect.then() function cannot block this thread. It maybe create another thread to run the codes in .then() function. Therefore, it will run WaitForSingleObjectEx(event_wait,INFINITE,false); this line directly, and not wait for .then() function.

    It has less relationship with
    baseFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
    Maybe this codes takes more time to let the .then() function to finish.

    If you want to block
    task<void> connect(socket->ConnectAsync(ref new HostName(IP), IP_port, SocketProtectionLevel::PlainSocket));
    In this function, you can try to use connect.wait();

    For more information, please check
    http://msdn.microsoft.com/en-us/library/windows/apps/dd492427(v=vs.110).aspx

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 15, 2012 7:50 AM
  • Dear Jesse

    Thanks you very much

    but if I use like this

    task<void> connect(socket->ConnectAsync(ref new HostName(IP), IP_port, SocketProtectionLevel::PlainSocket));

    connect.wait(); connect.get();

    it will pop-up follow error

    FileAccessSample.exe has triggered a breakpoint.If there is a handler for this exception, the program may be safely continued.

    Unhandled exception at 0x63D3978E (Windows.UI.Xaml.dll) in FileAccessSample.exe: 0xC0000602:  A fail fast exception occurred. Exception handlers will not be invoked and the process will be terminated immediately.


    Unhandled exception at 0x63D3609D (Windows.UI.Xaml.dll) in FileAccessSample.exe: 0xC0000005: Access violation reading location 0x00000008.


    Unhandled exception at 0x77CFF4DE (ntdll.dll) in FileAccessSample.exe: 0x00000000: The operation completed successfully.

    Thursday, March 15, 2012 10:17 AM
  • Hi,

    I would suggest you to use try-catch block to get the detail error messages.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, March 19, 2012 8:20 AM
  • Dear Jesse

    I have a question.

    if  I use Metro UI app to test StreamSocket receive data.

    when call LoadAsync() and try to read data from inputstream immediately.

    exception will show result 0x8000000b.

    but if let it go by itself ,the APP will read data after the main() exciting.  before UI changes to Metre UI

    so why about this 'this is my code

    auto task1 = make_task([=] { try { DataReaderLoadOperation^ rop; rop = smi_reader->LoadAsync(size); rop->Completed = ref new AsyncOperationCompletedHandler<unsigned int>([=](IAsyncOperation<unsigned int>^ a,AsyncStatus b) { try { smi_reader->ReadBytes(data); //memcpy(buf,data->Data,size); } catch (Exception^ e) { e->Message; } });  //todo get rop has completed and get data

    }

    catch (Exception^ e)
    {
    e->Message;
    }

    });

    structured_task_group tasks;

    tasks.wait();
    tasks.run_and_wait(task1);


     

    • Edited by oishixixi Sunday, March 25, 2012 10:01 AM
    Sunday, March 25, 2012 10:00 AM
  • Hello,

    DataReaderLoadOperation is a async function. You should call DataReaderLoadOperation.GetResults() function, so that the function will wait until it get the result or been canceled. If you did not call this function, the task will be run, maybe after main function. Because them are in different thread.

    In addition, for the structured_task_group Class. We should add the tasks through structured_task_group::run, and then wait them in structured_task_group::wait Method. 

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by oishixixi Wednesday, March 28, 2012 9:20 AM
    Monday, March 26, 2012 6:57 AM
  • Dear Jesse

    thank  you very much.

    I found the reason that I can't use ReadBytes() after LoadAsync() completed.

    because I test it in UI main thread. if I new a thread to do this test ,Readbytes() will not go to error.

    thank you.

    • Marked as answer by Jesse Jiang Thursday, March 29, 2012 2:25 AM
    Wednesday, March 28, 2012 9:24 AM