Canceling an Asynchronous File Read Operation

Login to reply to this topic.
Fri, 2005-09-02 08:47
Joined: 2005-06-24
Forum posts: 5
Hi All,

I have one requirement like, I want to issue Asychronous File Read/Write operation. Then if the request is still pending, I want o cancel that operation. This in deed should make sure that File pointer will be proper, in the sense that its still pointing to the same offset which it was before calling my Async Read/Write.

May be my code looks something like this:

_LIT( KFileName, "C:\\NewFile.txt");
RFs fileSession;
User::LeaveIfError(fileSession.Connect());
RFile file;
User::LeaveIFError(file.Open(Open(fileSession, KFileName, EFileRead | EFileShareAny));

//Get the current offset.
TInt offset = 0;
User::LeaveIfError(file.Seek(ESeekCurrent, offset));

//Read Data
TBuf8<600> readData;
TRequestStatus readStatus;
file.Read(readData, readCount, readStatus);

if(readStatus == KRequestPending)
{
/*
Now cancel the Read operation.
I dont know how to implement this part. Some one please help me.
I have tried with this code,
   TRequestStatus* sp = &readStatus;
   //User::RequestComplete(sp, KErrCancel);
   RThread().RequestComplete(sp, KErrCancel);
After this readStatus was KErrCancel, but after sometimes it changes to KErrNone as Server complets Asyn Read successfully.
*/
   TInt newOffset = 0;
   User::LeaveIfError(file.Seek(ESeekCurrent, newOffset));
   if(newOffset != offset)
      {
      //It was not properly cancelled !!!
      }
}



Now, please please..  some one help me implementing the Read Cancel, if its practically possible


Thanks a lot in advance
Girish

Girish Shetty


Fri, 2005-09-02 10:00
Joined: 2005-06-24
Forum posts: 5
Re: Canceling an Asynchronous File Read Operation
More info on the query....

For RSocket, we have CancelAccept(), CancelAll(), CancelConnect(), CancelIoctl(), CancelRead(), CancelRecv(), CancelSend(), CancelWrite() using which we can cancel all Async operations. But nothing is there in case of RFile.

Please let me know, if u have any idea.
One idea is setting the offset back using Seek,

      //Seek back to old position
      TInt prevPosition = offset-newOffset;
      retVal = file.Seek(ESeekCurrent, prevPosition);

But this is not a good idea as its not read cancel, its just forcing the operation to go back. More over, it requires me to wait till the completion of my async operation, else I may end up with some wrong file state (offset)

Thanks
Girish




Girish Shetty

  • Login to reply to this topic.