我要做一个文件加密组件,主要实现以下方法:
EncryptFile(...)加密指定文件函数(算法已经确定)
IsFileEncrypted(....)检查指定文件是否已经加密
IsWorking(...)检查加密是否正在进行
Progress获取加密进度的属性组件主要用于网页,其次exe程序。由于加密需要时间长,所以不知道是不是应该在EncryptFile函数里新建线程。另外组件用什么线程模式?Apartment还是Single.为什么?请指教。

解决方案 »

  1.   

    Threading Revisited
    After a careful examination of the implementation of the TipOfTheDay coclass, we confirmed our original suspicions: we were able to design the component such that it doesn't store global data, per-class data, or even per-instance data. The state of the component is entirely determined by the cookie parameter stored by the client application, so restricting object access to a single thread is unnecessary. Because the TipOfTheDay component doesn't use worker threads, there's no reason we wouldn't allow it to be created in an STA, so it makes sense to support both threading models. We'll change the Registry setting from ThreadingModel=Apartment to ThreadingModel=Both by editing the TIPOFTHEDAY.RGS file that the ATL Object Wizard created and added to the project.
    InprocServer32 = s `%MODULE%'
    {
        val ThreadingModel = s `Both'

    We'll also need to change some wizard-generated code to reflect the threading model. Replace
    #define _ATL_APARTMENT_THREADED
    Public CComObjectRootEx<CComSingleThreadModel> 
    with
    #define _ATL_FREE_THREADED
    Public CComObjectRootEx<CComMultiThreadModel> NOTE
    --------------------------------------------------------------------------------
     It wouldn't be accurate to say that the TipOfTheDay component is stateless, because the call history does indeed affect the data returned by the GetNextTip method. A client that calls GetNextTip three times will get a different result than a client that calls GetNextTip four times. More precisely, our component can be called "state estranged." A separate entity manages and stores its state.
      

  2.   

    ThreadingModel=Apartment
    The apartment model (or models) supported by an in-process component is determined by its ThreadingModel Registry setting. Although the wizard makes it easy for you to choose any one of these models, you shouldn't make this decision lightly. If you specify an STA—which, somewhat confusingly, maps to ThreadingModel=Apartment in the Registry—you're guaranteed that instances of your component will be accessed by only a single thread. That means you must protect access to global data but not to instance data. If you specify the MTA (ThreadingModel=Free), you must protect access to both global and instance data. Because we're not sure whether the TipOfTheDay component will maintain per-instance state, we'll initially  it as ThreadingModel=Apartment to be on the safe side. After we've completed the implementation, we'll reexamine the issue to see whether individual instances of the TipOfTheDay component can handle concurrent access by multiple threads. If so, it might make sense to choose ThreadingModel=Both instead.