locked
How to use ThreadPool with idl importing

    Question

  • Hi all, 

    I have a problem about referencing winRT components.

    I try to import Windows.System.Threading by adding the following in my winRT idl file

     

    import "threadpoolwinrt.idl";

     


    After compiling the idl, I was able to use the namespace Windows.System.Threading, but I can't see the ThreadPool. And the threadpoolwinrt.idl shows that the ThreadPool is not inheriting the IInspectable and thus not a activatable class.

    According to http://social.msdn.microsoft.com/Forums/en-US/wingameswithdirectx/thread/b8fdd906-4bfd-498f-87f1-606cc2b20c58. ThreadPool should be ready to be used after

     

    #using "Windows.System.Threading.winmd"
    
    using namespace Windows::System::Threading;


    Since the winmd comes from the idl, why these two methods show different behaviors? Can I import the ThreadPool without using the second method(it requires /clr)? 

     

    Thanks.

     

    Tuesday, November 1, 2011 2:31 PM

Answers

  • If you are using WRL, you will have to do something like:

    class WorkItemCallback :
        public RuntimeClass<
        RuntimeClassFlags<...>,
        IWorkItemHandler>
    {
    .....
    }
    
    
    // and later in your code
    
        ComPtr<Windows::Foundation::IAsyncAction> runLong;
        ComPtr<IWorkItemHandler> handler;
    
        MakeAndInitialize<WorkItemCallback>(&handler, ..., ...);
        m_spThreadpoolStatics->RunAsync(handler.Get(), &runLong);
    



    Raman Sharma | Program Manager, Visual C++ | @rasharm_msft

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)
    Tuesday, November 8, 2011 7:53 PM
  • OK, so the problem is that the runtimeclass defined in the IDL is [webhosthidden] means it is not available in JavaScript.

     

        [version(NTDDI_WIN8), static(IThreadPoolStatics, NTDDI_WIN8)]
        [webhosthidden]
        runtimeclass ThreadPool
        {
        }
    
    


    Second, it is a static class so it is not activatable.  You have to look at its static interface:

     

        [version(NTDDI_WIN8)]
        [uuid(b6bf67dd-84bd-44f8-ac1c-93ebcb9dba91)]
        [webhosthidden]
        [exclusiveto(ThreadPool)]
        interface IThreadPoolStatics : IInspectable
        {
            [overload("RunAsync")]
            HRESULT
            RunAsync (
                [in] WorkItemHandler * handler,
                [out, retval] Windows.Foundation.IAsyncAction ** operation
                );
    
            [overload("RunAsync")]
            HRESULT
            RunWithPriorityAsync (
                [in] WorkItemHandler * handler,
                [in] WorkItemPriority priority,
                [out, retval] Windows.Foundation.IAsyncAction ** operation
                );
    
        }
    
    


     

    So this means in CPP you get a reference to the static class:

    ComPtr<IThreadPoolStatics> spThreadpoolStatics;
           
    HSTRING classString = CreateHString(RuntimeClass_Windows_System_Threading_ThreadPool);
    HRESULT hr = Windows::Foundation::GetActivationFactory(classString, &spThreadpoolStatics);
    
    

    And then you can call into it to schedule work:

           spThreadpoolStatics->RunAsync(handler, &operation);
    

     

    Does that help?

     


    Raman Sharma | Program Manager, Visual C++ | @rasharm_msft

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)
    Thursday, November 3, 2011 5:05 PM

All replies

  • ThreadPool is a static class (and not a runtime object) under the namespace Windows.System.Threading.  You use it's methods in C++ like this:

    ThreadPool::RunAsync

    What exactly does your IDL file look like?

    Tuesday, November 1, 2011 5:02 PM
  • Hi Raman,

    Here is my idl

    #include <sdkddkver.h>
    import "Windows.Foundation.idl";
    import "threadpoolwinrt.idl";
    
    namespace Samples.Effects
    {
        // Forward declarations
        runtimeclass CGrayscale;
    
        // Interface definitions
        [version(1.0), uuid(332FD2F1-1C69-4C91-949E-4BB67ABDC585)]
        interface IGrayscale : IInspectable
        {
            [propget] HRESULT Level([out, retval] DWORD* pdwLevel);
        }
    
        [version(1.0), activatable(1.0)]
        runtimeclass CGrayscale
        {
            [default] interface IGrayscale;
        }
    
    }
    

     

    And I can use the namespace Windows.System.Threading, but ThreadPool is not in there. 

    Wednesday, November 2, 2011 2:20 AM
  • Actually there seems to be some discrepancy between the header file (threadpoolwinrt.h) and the winmd file (Windows.System.Threading.winmd).  The header file is missing the definition of the "class ThreadPool" which causes you not to see it when you are trying to use it.

    Whereas, the winmd file and the IDL file (threadpoolwinrt.idl) have this class defined.

    Let me check what's going on.

    Thanks
    Raman 

     


    Raman Sharma | Program Manager, Visual C++ | @rasharm_msft

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)
    Wednesday, November 2, 2011 7:39 AM
  • OK, so the problem is that the runtimeclass defined in the IDL is [webhosthidden] means it is not available in JavaScript.

     

        [version(NTDDI_WIN8), static(IThreadPoolStatics, NTDDI_WIN8)]
        [webhosthidden]
        runtimeclass ThreadPool
        {
        }
    
    


    Second, it is a static class so it is not activatable.  You have to look at its static interface:

     

        [version(NTDDI_WIN8)]
        [uuid(b6bf67dd-84bd-44f8-ac1c-93ebcb9dba91)]
        [webhosthidden]
        [exclusiveto(ThreadPool)]
        interface IThreadPoolStatics : IInspectable
        {
            [overload("RunAsync")]
            HRESULT
            RunAsync (
                [in] WorkItemHandler * handler,
                [out, retval] Windows.Foundation.IAsyncAction ** operation
                );
    
            [overload("RunAsync")]
            HRESULT
            RunWithPriorityAsync (
                [in] WorkItemHandler * handler,
                [in] WorkItemPriority priority,
                [out, retval] Windows.Foundation.IAsyncAction ** operation
                );
    
        }
    
    


     

    So this means in CPP you get a reference to the static class:

    ComPtr<IThreadPoolStatics> spThreadpoolStatics;
           
    HSTRING classString = CreateHString(RuntimeClass_Windows_System_Threading_ThreadPool);
    HRESULT hr = Windows::Foundation::GetActivationFactory(classString, &spThreadpoolStatics);
    
    

    And then you can call into it to schedule work:

           spThreadpoolStatics->RunAsync(handler, &operation);
    

     

    Does that help?

     


    Raman Sharma | Program Manager, Visual C++ | @rasharm_msft

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)
    Thursday, November 3, 2011 5:05 PM
  •  I can access the ThreadPool. Thanks a lot!

     

    But the problem goes to: how to access the delegate method WorkItemHandler to create a handler for the RunAsync? The WorkItemHandler is also missing in the namespace.

     Can you kindly give me some hint or reference that I can dig into? 

    Thanks again.

     

     

    Friday, November 4, 2011 3:27 PM
  • If you are using WRL, you will have to do something like:

    class WorkItemCallback :
        public RuntimeClass<
        RuntimeClassFlags<...>,
        IWorkItemHandler>
    {
    .....
    }
    
    
    // and later in your code
    
        ComPtr<Windows::Foundation::IAsyncAction> runLong;
        ComPtr<IWorkItemHandler> handler;
    
        MakeAndInitialize<WorkItemCallback>(&handler, ..., ...);
        m_spThreadpoolStatics->RunAsync(handler.Get(), &runLong);
    



    Raman Sharma | Program Manager, Visual C++ | @rasharm_msft

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)
    Tuesday, November 8, 2011 7:53 PM