Solved

"Handle does not support asynchronous operations"

Posted on 2004-08-11
5
1,914 Views
Last Modified: 2010-05-19
I'm using a mixture between managed code and pure C# ;) ...

So I'm opening a file (actually it's not a file but an USB HID) with:

                  HidHandle = CreateFile(
                        DeviceName,
                        GENERIC_READ | GENERIC_WRITE,
                        FILE_SHARE_READ | FILE_SHARE_WRITE,
                        0,
                        OPEN_EXISTING,
                        FILE_FLAG_OVERLAPPED,
                        0);


I get a valid Handle and I use this handle to:
FileStream fs = new FileStream(xx,FileAccess.ReadWrite,true,7,true);
where xx is this handle (previously converted to a IntPtr)

Now I can use
fs.Read (...)
to get one block of data (7 bytes)

but when I want to read a second time, I get the message:

"An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: Handle does not support asynchronous operations.  The parameters to the FileStream constructor may need to be changed to indicate that the handle was opened synchronously (that is, it was not opened for overlapped I/O)."

The Overlapped Flag is defined as:
private const int FILE_FLAG_OVERLAPPED = 0x40000000;


Does anyone have any idea ?

0
Comment
Question by:somnatic
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:davidastle
ID: 11788303
Try changing the last parameter in the FileStream constructor (isAsync) to false
0
 
LVL 2

Assisted Solution

by:davidastle
davidastle earned 500 total points
ID: 11788311
IE FileStream fs = new FileStream(xx,FileAccess.ReadWrite,true,7,false);
0
 
LVL 4

Author Comment

by:somnatic
ID: 11790586
hmm .. Sorry, I can't ...

Since I need that operation to be asynchronous.
 
0
 
LVL 2

Accepted Solution

by:
davidastle earned 500 total points
ID: 11793134
really?  fs.Read() is a synchronous operation...

First of all,
"FileStream provides two different modes of operation: synchronous I/O and asynchronous I/O. While either can be used, the underlying operating system resources might allow access in only one of these modes."
"Windows 98, Windows Millennium Edition Platform Note:  These operating systems do not support asynchronous I/O. If code for these platforms requests that files be opened asynchronously, the common language runtime will open them synchronously instead"
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofilestreamclassisasynctopic.asp)

Also, try locking the stream when you read from it.

You could try the asynchronous read... maybe windows is stupid and will only allow you to do an async read if you try to open async... To this, use BeginRead().
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofilestreamclassbeginreadtopic.asp
Has an example on how to this.

If all alse fails, you can try to force asnyc operations by creating your fs.Read function on a new thread.  You will probably have to lock the device to read from it though, judging from the message it gave you.
0
 
LVL 4

Author Comment

by:somnatic
ID: 11807235
thanks davidastle ...

I always thought Read would give me both, asynchronous and synchronous operation ... I don't know why I thought so :)

I switched my project now to synchronous operation and I use two different handles to the USB Device (one for writing, one for reading) .. this gives me two different FileStream objects .. This works for me quite well (only one problem left, but you can find another thread on this her on ee).


Thanks for the help from you both :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question