locked
The ThreadPool::RunAsync calls work items asynchronously, but in the same thread!

    Question

  • I try to open some threads to perform a portable device operations asynchronously. It works, but my worker-handler are called in the same thread as the main-GUI. I do something wrong?

    void MyDataProvider::OnCmdOpenDevice(Object^ parameter)
    {
    	if(m_spOpenDriveThread != nullptr)
    	{
    		::WaitForSingleObject(m_evtOpenDriveDown, INFINITE);
    		::ResetEvent(m_evtOpenDriveDown);
    	}
    	else
    	{
    		if(m_evtOpenDriveDown == nullptr)
    			m_evtOpenDriveDown = ::CreateEventW(nullptr, true, false, nullptr);
    		else
    			::ResetEvent(m_evtOpenDriveDown);
    	}
    
    	m_spOpenDriveThread = ThreadPool::RunAsync(ref new WorkItemHandler([this](Windows::Foundation::IAsyncAction^ spAction)
    	{
                      // I expect that I'm here in another thread, but it is not
    		ClearDriveList();
    
    		ComPtr<IMxWPD_Manager> spManager;
     		if(Core::Get(&spManager))
    		{
    			ComPtr<IMxWPD_DevEnum> spEnumDev;
    			EMxWPD_Error xError = spManager->GetEnumDevices(&spEnumDev);
    			if(xError == ER_NoError)
    			{
    				UINT nCountDev = spEnumDev->GetCount();
    				for(UINT i = 0; i < nCountDev; i++)
    				{
    					ComPtr<IMxWPD_Device> spDevice;
    					xError = spEnumDev->GetAt(i, &spDevice);
    					if(xError == ER_NoError)
    					{
    						BSTR strFriendlyName = nullptr, strManufacturer = nullptr, strDescription = nullptr, strDeviceID = nullptr;
    						xError = spDevice->GetIdentity(&strFriendlyName, &strManufacturer, &strDescription, &strDeviceID, nullptr);
    						if(xError == ER_NoError)
    						{
    							AppendNewDrive(ref new String(strFriendlyName), ref new String(strDeviceID));
    
    							::SysFreeString(strFriendlyName);
    							::SysFreeString(strManufacturer);
    							::SysFreeString(strDescription);
    							::SysFreeString(strDeviceID);
    						}
    					}
    				}
    			}
    		}
    
    		m_spOpenDriveThread = nullptr;
    		::SetEvent(m_evtOpenDriveDown);
    	}));
    	m_spOpenDriveThread->Start();
    }
    

    Tuesday, November 15, 2011 7:52 AM

Answers

  • You should achieve the results your looking for by adding Platform::CallbackContext::Any. An example:

     

    IAsyncAction^ threadPoolWorkItem = ThreadPool::RunAsync(ref new WorkItemHandler(
    [=] (IAsyncAction^ operation)
    {
    //Stuff to do on threadpool thread 
    }, Platform::CallbackContext::Any)
    );
    

     


    Thursday, November 17, 2011 2:14 AM
    Moderator

All replies

  • I have translated the "Thread Pool Sample" example in C++ and it also does not work. All of the work item functions are called in the same UI-thread.
    I'm totally confused! Please help :-)
    • Edited by AnryNet Wednesday, November 16, 2011 3:30 PM
    Wednesday, November 16, 2011 3:27 PM
  • You should achieve the results your looking for by adding Platform::CallbackContext::Any. An example:

     

    IAsyncAction^ threadPoolWorkItem = ThreadPool::RunAsync(ref new WorkItemHandler(
    [=] (IAsyncAction^ operation)
    {
    //Stuff to do on threadpool thread 
    }, Platform::CallbackContext::Any)
    );
    

     


    Thursday, November 17, 2011 2:14 AM
    Moderator
  • Yuo, Its working...!

    1000 Thanks David

    Thursday, November 17, 2011 6:58 AM
  • Glad to hear your working.

     

    If you have the Thread Pool sample in C++ working, that would be a great addition to the Metro Samples.

     

    Thanks,

    -David

     

    Thursday, November 17, 2011 9:32 PM
    Moderator
  • Hi David,

    Your response above speaks to "the Thread Pool sample", which I'm reading as meaning that there is a Thread Pool sample available that is written in C, since you suggest that a working C++ versino would be a great addition to the Metro Samples.

    Could you provide a link to these Metro Samples, and in particular, to a C sample that uses the thread pool?

    Thanks!

    Bob


    Bob Mance

    Tuesday, April 03, 2012 2:31 PM
  • Hi Bob,

    You can find all of the Metro style app samples by clicking the "Samples" link at the top of the page. Searching on "thread pool" will find the Thread pool sample.

    I am not aware of any Metro style app samples in C. I suspect AnryNet was translating from C#. The Consumer Preview samples include a C++ one.

    --Rob

    Tuesday, April 03, 2012 4:31 PM
    Owner