Cancel Flow

Hi
I’m developing a WDM driver and I need to implement Cancel Flow in my
Driver.

Lets say that I have IOCTL that takes a lot of time to run. (Polling
Hardware bit until I have answer from my device that can take up to 1
min for example, I know that it is not a good idea to use polling but
let’s say I do)

In the driver I have IRPs Queue and I’m setting a cancel routine to the
IRP’s before I’m adding them to the queue.
all what the Cancel Routine need to do is to abort the operation of the
IOCTL (using Event/Flag or etc)

Now, I developed a console test application that have two threads, in
the first thread I Open the Device (using CreateFile with
FILE_FLAG_OVERLAPPED flag) then Create the Second Thread and then call
DeviceIoControl in Overlapped mode. (I’m also using
GetOverlappedResult() to wait until I got the data)
From the another thread I’m sleeping for few seconds and then calling
CancelIo().

For some reason the Cancel Routine is not Called when I run the test
application and I don’t know why.
If I force close the test Application (Pressing the X of the window or
Killing it from Task Manager) the Cancel Routine is called and work fine.

Can someone please help me?

Thanks in Advance for all of the answers.


Oren Weil

CancelIo only cancels I/Os from the calling thread. Your calling thread did
not initiate the I/O.

Bill Wandel

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com]
On Behalf Of Oren Weil
Sent: Monday, January 14, 2008 4:52 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Cancel Flow

Hi
I’m developing a WDM driver and I need to implement Cancel Flow in my
Driver.

Lets say that I have IOCTL that takes a lot of time to run. (Polling
Hardware bit until I have answer from my device that can take up to 1 min
for example, I know that it is not a good idea to use polling but let’s say
I do)

In the driver I have IRPs Queue and I’m setting a cancel routine to the
IRP’s before I’m adding them to the queue.
all what the Cancel Routine need to do is to abort the operation of the
IOCTL (using Event/Flag or etc)

Now, I developed a console test application that have two threads, in the
first thread I Open the Device (using CreateFile with FILE_FLAG_OVERLAPPED
flag) then Create the Second Thread and then call DeviceIoControl in
Overlapped mode. (I’m also using
GetOverlappedResult() to wait until I got the data) From the another thread
I’m sleeping for few seconds and then calling CancelIo().

For some reason the Cancel Routine is not Called when I run the test
application and I don’t know why.
If I force close the test Application (Pressing the X of the window or
Killing it from Task Manager) the Cancel Routine is called and work fine.

Can someone please help me?

Thanks in Advance for all of the answers.


Oren Weil


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

OP,

On Vista see CancelIoEx(…). A quick fix might be just post a user mode APC to cancel the IO from the other thread.

CancelIo cancels io sent on the calling thread, not all threads. If this is vista, you can use CancelIoEx to cancel a particular request by specifying the correct OVERLAPPED

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Oren Weil
Sent: Monday, January 14, 2008 1:52 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Cancel Flow

Hi
I’m developing a WDM driver and I need to implement Cancel Flow in my
Driver.

Lets say that I have IOCTL that takes a lot of time to run. (Polling
Hardware bit until I have answer from my device that can take up to 1
min for example, I know that it is not a good idea to use polling but
let’s say I do)

In the driver I have IRPs Queue and I’m setting a cancel routine to the
IRP’s before I’m adding them to the queue.
all what the Cancel Routine need to do is to abort the operation of the
IOCTL (using Event/Flag or etc)

Now, I developed a console test application that have two threads, in
the first thread I Open the Device (using CreateFile with
FILE_FLAG_OVERLAPPED flag) then Create the Second Thread and then call
DeviceIoControl in Overlapped mode. (I’m also using
GetOverlappedResult() to wait until I got the data)
From the another thread I’m sleeping for few seconds and then calling
CancelIo().

For some reason the Cancel Routine is not Called when I run the test
application and I don’t know why.
If I force close the test Application (Pressing the X of the window or
Killing it from Task Manager) the Cancel Routine is called and work fine.

Can someone please help me?

Thanks in Advance for all of the answers.


Oren Weil


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Oren Weil wrote:

Hi
I’m developing a WDM driver and I need to implement Cancel Flow in my
Driver.

Lets say that I have IOCTL that takes a lot of time to run. (Polling
Hardware bit until I have answer from my device that can take up to 1
min for example, I know that it is not a good idea to use polling but
let’s say I do)

In the driver I have IRPs Queue and I’m setting a cancel routine to
the IRP’s before I’m adding them to the queue.
all what the Cancel Routine need to do is to abort the operation of
the IOCTL (using Event/Flag or etc)

Now, I developed a console test application that have two threads, in
the first thread I Open the Device (using CreateFile with
FILE_FLAG_OVERLAPPED flag) then Create the Second Thread and then call
DeviceIoControl in Overlapped mode. (I’m also using
GetOverlappedResult() to wait until I got the data)
From the another thread I’m sleeping for few seconds and then calling
CancelIo().

For some reason the Cancel Routine is not Called when I run the test
application and I don’t know why.

Because you didn’t read the documentation carefully enough. CancelIo
only cancels I/O requests made from that very same thread. It doesn’t
affect calls made from other threads.

You’ll have to call CancelIo from the same thread that called
DeviceIoControl.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

But it is the same process, I’m not getting Error with the CancelIo().

Bill Wandel wrote:

CancelIo only cancels I/Os from the calling thread. Your calling thread did
not initiate the I/O.

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com]
On Behalf Of Oren Weil
Sent: Monday, January 14, 2008 4:52 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Cancel Flow

Hi
I’m developing a WDM driver and I need to implement Cancel Flow in my
Driver.

Lets say that I have IOCTL that takes a lot of time to run. (Polling
Hardware bit until I have answer from my device that can take up to 1 min
for example, I know that it is not a good idea to use polling but let’s say
I do)

In the driver I have IRPs Queue and I’m setting a cancel routine to the
IRP’s before I’m adding them to the queue.
all what the Cancel Routine need to do is to abort the operation of the
IOCTL (using Event/Flag or etc)

Now, I developed a console test application that have two threads, in the
first thread I Open the Device (using CreateFile with FILE_FLAG_OVERLAPPED
flag) then Create the Second Thread and then call DeviceIoControl in
Overlapped mode. (I’m also using
GetOverlappedResult() to wait until I got the data) From the another thread
I’m sleeping for few seconds and then calling CancelIo().

For some reason the Cancel Routine is not Called when I run the test
application and I don’t know why.
If I force close the test Application (Pressing the X of the window or
Killing it from Task Manager) the Cancel Routine is called and work fine.

Can someone please help me?

Th


Oren Weil

Yes, you have multiple threads in the same process, but CancelIo is does not effect all threads operating on the same file handle, only i/o sent on the calling thread for the particular file handle. In the thread that sends the DeviceIoControl do not infinitely block. After a timeout period, call CancelIo in this thread. It will then cancel the IOCTL request. CancelIo is not returning an error b/c there is no error, there was nothing to cancel so it canceled nothing and returned success (the only way this api can really return an error is if you do not pass a valid file handle to it).

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Oren Weil
Sent: Monday, January 14, 2008 9:04 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Cancel Flow

But it is the same process, I’m not getting Error with the CancelIo().

Bill Wandel wrote:

CancelIo only cancels I/Os from the calling thread. Your calling thread did
not initiate the I/O.

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com]
On Behalf Of Oren Weil
Sent: Monday, January 14, 2008 4:52 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Cancel Flow

Hi
I’m developing a WDM driver and I need to implement Cancel Flow in my
Driver.

Lets say that I have IOCTL that takes a lot of time to run. (Polling
Hardware bit until I have answer from my device that can take up to 1 min
for example, I know that it is not a good idea to use polling but let’s say
I do)

In the driver I have IRPs Queue and I’m setting a cancel routine to the
IRP’s before I’m adding them to the queue.
all what the Cancel Routine need to do is to abort the operation of the
IOCTL (using Event/Flag or etc)

Now, I developed a console test application that have two threads, in the
first thread I Open the Device (using CreateFile with FILE_FLAG_OVERLAPPED
flag) then Create the Second Thread and then call DeviceIoControl in
Overlapped mode. (I’m also using
GetOverlappedResult() to wait until I got the data) From the another thread
I’m sleeping for few seconds and then calling CancelIo().

For some reason the Cancel Routine is not Called when I run the test
application and I don’t know why.
If I force close the test Application (Pressing the X of the window or
Killing it from Task Manager) the Cancel Routine is called and work fine.

Can someone please help me?

Th


Oren Weil


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

> From the another thread I’m sleeping for few seconds and then calling

CancelIo().

CancelIo is documented to be per-thread, so, calling it from another thread
will not give you anything.


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com