问题
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:
- What is the correct way to stop asynchronous search job for updates?
- Also i misunderstood what is difference between ISearchJob::CleanUp and ISearchJob::RequestAbort and how to use this methods to stop asynchronous search correctly?
- 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