3

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?
IInspectable
  • 46,945
  • 8
  • 85
  • 181
Space Rabbit
  • 141
  • 2
  • 11

1 Answers1

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.

andlabs
  • 11,290
  • 1
  • 31
  • 52
  • I use this code in destructor of my class, which wraps WUAPI. – Space Rabbit Oct 13 '16 at 14:04
  • 1
    What will be if I will do only `pUpJob_->RequestAbort()` without `pUpSearcher_->EndSearch()` – Space Rabbit Oct 13 '16 at 14:07
  • @SpaceRabbit just curious what was your resolution to this situation? My algorithm does this when the search needs to be aborted due to program exiting: (1) BeginSearch (2) RequestAbort (3) Cleanup (4) NO EndSearch. The problem is sometimes Cleanup() crashes! I wonder if you even need to call Cleanup at all when RequestAbort is used? – fmike7 Jun 03 '21 at 18:08
  • @SpaceRabbit would be great if you share your final resolution to this issue here in this thread, as I am running into a similar issue now. But in my case CleanUp() crashes! – fmike7 Jun 03 '21 at 20:36