Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Aborting synchronous function call,....

3 views
Skip to first unread message

Kerem Gümrükcü

unread,
Dec 20, 2009, 12:54:01 PM12/20/09
to
Hi,

what is the best way to abort a synchronous function call or
better to say make the call abortable,...

Regards

Kerem

--
-----------------------
Beste Gr�sse / Best regards / Votre bien devoue
Kerem G�mr�kc�
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------

Joseph M. Newcomer

unread,
Dec 20, 2009, 2:00:51 PM12/20/09
to
There isn't, not really. You would have to give a whole lot more context to get a
reasonable answer.

The usual method of aborting a call sequence is to have, at at least one (the bottommost)
but possibly at several levels a test of a boolean value, and if the boolean is in the
"abort" state, to do something that causes this condition to propagate upwards. It may be
as simple as returning FALSE or NULL or an empty string, or in C++, you would typically do
a 'throw' of some user-defined exception. In MFC, it would most likely be an exception
derived from the CException class.

Of course, this assumes that you are in a mode where you want to abort some long
computation that is not hung on a kernel call. Handling aborts with kernel calls involved
is yet a different question. If the call is an I/O call, Vista provides a
CancelSynchronousIo call to abort the transaction, but this involves a second thread (see
below). If by "abort" you mean "I've discoverer a problem deep inside the computation and
want to terminate the whole mess" the usual method is to throw an exception.

If you have a computation that can go on for too long, but does not involve a kernel call,
such a situation implies that there is a separate thread that is going to set the boolean
value. Therefore, if you are doing this in the main GUI thread, the general answer is
"there is no way". Most commonly, if you have this situation, you will spin off a thread
to do the computation, and your main GUI thread has some kind of means of terminating the
computation (a modal dialog box with a "cancel" button; a modeless dialog box, ditto; a
menu item; a control on a dialog or CFormView-derived class, etc.). It would be a poor
choice to do this potentially-abortable call in your main GUI thread.

So you need to say a lot more about the nature of the problem context you are working in.
joe

On Sun, 20 Dec 2009 18:54:01 +0100, Kerem G�mr�kc� <kare...@hotmail.com> wrote:

>Hi,
>
>what is the best way to abort a synchronous function call or
>better to say make the call abortable,...
>
>Regards
>
>Kerem

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

m

unread,
Dec 20, 2009, 5:55:14 PM12/20/09
to
Obviously, this depends on the function in question. If the API is in
Windows or a third party library, then the best advise I can give is don't
try.

If you want to write or modify one of your own APIs to be abortable, then I
can suggest that this is commonly done by replacing a call to
WaitForSingleObject with a call to WaitForMultipleObects + CancelIo & the
abort achieved by a call to SetEvent. Of course, the implementation details
of your API will dictate how cancelation should proceed since it depends on
where in the execution you want the call to be cancelable - clearly, there
will be some places that will execute before cancelation is possible, and
after it is no longer possible.

"Kerem G�mr�kc�" <kare...@hotmail.com> wrote in message
news:O6zbp1Zg...@TK2MSFTNGP05.phx.gbl...

Joseph M. Newcomer

unread,
Dec 20, 2009, 7:43:54 PM12/20/09
to
Actually, the OP did not ask about an API, just a function call. Which is one of the
reasons I asked for more explanation, since if someone had really meant to ask about an
API, they would have explicitly said "API", and most likely give the specific API they
were concerned about, in which case it would be possible to formulate an answer.

The question was poorly-formed, vague at best.
joe

Kerem Gümrükcü

unread,
Dec 21, 2009, 2:35:52 AM12/21/09
to
Hi Joe,

thanks for the long and detailed answer and i must
apolgize here that i did not tell that i am talking
about a windows api function already compiled and
inside a windows api dll. I am talking about the
NetServerEnum() which sometimes needs extremly
long to discover the data i requested from it. The
same problem comes with WNetEnumResource.
Both functions are blocking the entire UI so i
decided to move them to a extra thread and work
signaling/events to notify its success or failure. Thats
no problem so far but how do i abort the function call
inside the thread. Due to the nature of the call i have
to wait until it returns, but is there some way NOT
to wait and abort the function call and all its subsequent
calls in the stack. AFIAK know this is NOT possible and
i have to wait until it comes back,...

Regards

Kerem


--
-----------------------
Beste Gr�sse / Best regards / Votre bien devoue
Kerem G�mr�kc�
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------

"Joseph M. Newcomer" <newc...@flounder.com> schrieb im Newsbeitrag
news:imssi511o1m2mqe6q...@4ax.com...

Goran

unread,
Dec 21, 2009, 3:47:10 AM12/21/09
to
On Dec 21, 8:35 am, Kerem Gümrükcü <kareem...@hotmail.com> wrote:
> Hi Joe,
>
> thanks for the long and detailed answer and i must
> apolgize here that i did not tell that i am talking
> about a windows api function already compiled and
> inside a windows api dll. I am talking about the
> NetServerEnum() which sometimes needs extremly
> long to discover the data i requested from it. The
> same problem comes with WNetEnumResource.
> Both functions are blocking the entire UI so i
> decided to move them to a extra thread and work
> signaling/events to notify its success or failure. Thats
> no problem so far but how do i abort the function call
> inside the thread. Due to the nature of the call i have
> to wait until it returns, but is there some way NOT
> to wait and abort the function call and all its subsequent
> calls in the stack. AFIAK know this is NOT possible and
> i have to wait until it comes back,...

Correct, this is NOT possible. You can do stuff in another thread to
keep UI alive, as you do, and you have to wait, eventually, for
another thread to end.

People who have a different "solution" for this problem have no clue
what they are talking about (e.g. there is TerminateThread API that
should not, EVER, be used for such purposes).

Goran.

Joseph M. Newcomer

unread,
Dec 21, 2009, 10:57:42 AM12/21/09
to
There is no known way to terminate this early, unless, in Vista+, the handle could be used
with a CancelSynchronousIo call. I have no idea what the nature of the HANDLE type is,
and therefore don't know if this is a feasible approach. This could not be done in any
version earlier than Vista.

In either case, where you have to wait or even if the CancelSynchronousIo actually does
work, you would have to do this in a separate thread. Since it is well-known that doing
long blocking calls in the main GUI thread is bad practice, the only solution is to not do
long blocking calls in the main GUI thread. So you will want to make this call from a
separate thread.

Otherwise, there is no way to "keep the GUI alive". You may also want to disable certain
menu items, toobar items and/or dialog/CFormView-derived controls while that thread is
running. When the thread completes, it can notify you that it has completed; as the
resources are successfully enumerated, you would want to PostMessage to the main GUI
thread if you want the information displayed in the GUI. In addition, if it completes
successfully, you not only want to enable controls that made no sense to be enabled while
it was running (such as the control which initiates the operation) but enable controls
that are now viable because they have data.

In one case I had, where it took several minutes to enumerate all the embedded systems in
a network, I actually allowed the user to click on whatever systems had been found and
start interacting with them even while the enumeration was proceeding for the entire
embedded network. What you do in your case is up to you.
joe

On Mon, 21 Dec 2009 08:35:52 +0100, Kerem G�mr�kc� <kare...@hotmail.com> wrote:

>Hi Joe,
>
>thanks for the long and detailed answer and i must
>apolgize here that i did not tell that i am talking
>about a windows api function already compiled and
>inside a windows api dll. I am talking about the
>NetServerEnum() which sometimes needs extremly
>long to discover the data i requested from it. The
>same problem comes with WNetEnumResource.
>Both functions are blocking the entire UI so i
>decided to move them to a extra thread and work
>signaling/events to notify its success or failure. Thats
>no problem so far but how do i abort the function call
>inside the thread. Due to the nature of the call i have
>to wait until it returns, but is there some way NOT
>to wait and abort the function call and all its subsequent
>calls in the stack. AFIAK know this is NOT possible and
>i have to wait until it comes back,...
>
>Regards
>
>Kerem

Pavel A.

unread,
Dec 21, 2009, 7:42:16 PM12/21/09
to
"Kerem G�mr�kc�" <kare...@hotmail.com> wrote in message
news:O6zbp1Zg...@TK2MSFTNGP05.phx.gbl...

> Hi,
>
> what is the best way to abort a synchronous function call or
> better to say make the call abortable,...


On Vista+ there's a new API, CancelSynchronousIo to cancel functions that
were not
cancelable before (like CreateFile). But this is only for NT6.

--pa

Joseph M. Newcomer

unread,
Dec 21, 2009, 9:47:33 PM12/21/09
to
And to use this for WNetEnumresources it would work only if the HANDLE is an I/O handle,
and not a HANDLE to some internal data structure.
joe

0 new messages