Correct way to stop asynchronous ISearchJob

吃可爱长大的小学妹 提交于 2021-02-10 06:33:12

问题


I am going to use WUA API and begin execution of an asynchronous search for updates in this way:

CComPtr<SearchCallbackImpl> iscc_; <<-- Note you need to CreateInstance
CComPtr<ISearchJob> pUpJob_;
pUpJob_ = NULL;

pUpSearcher_->BeginSearch(
        CComVariant(criteria.c_str()).bstrVal,
        iscc_,
        CComVariant(L"Scanning"),
        &pUpJob_);

When I need to stop my program, but ISearchJob has not completed yet, I use this code:

if (pUpJob_)
{
    CComVariant isStopped;
    pUpJob_->get_IsCompleted(&isStopped.boolVal);
    if (isStopped.boolVal == VARIANT_FALSE)
    {
        if (SUCCEEDED(pUpJob_->RequestAbort()))
        {
            pUpJob_->CleanUp();
            pUpJob_.Release();
        }
    }
}

Generally this code works but sometime it hangs on pUpJob_->CleanUp(); and I do not have ability to stop my programm correctly.

So my questions are:

  1. What is the correct way to stop asynchronous search job for updates?
  2. Also i misunderstood what is difference between ISearchJob::CleanUp and ISearchJob::RequestAbort and how to use this methods to stop asynchronous search correctly?
  3. Should this methods be used together or separately?

回答1:


RequestAbort() is also asynchronous (the hint to that is in the name). After calling it, you should call pUpSearcher_->EndSearch(); it will return an ISearchResult with ResultCode equal to orcAborted if the abort was successful. Then you can free your resources.

I'm not fully sure how CleanUp() is supposed to be used, but this page seems to imply it's intended for scripts that have callbacks, and that you're not supposed to call CleanUp() from within a callback. Not sure where your code for cancelling is run.



来源:https://stackoverflow.com/questions/40018357/correct-way-to-stop-asynchronous-isearchjob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!