Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

"Handle does not support asynchronous operations"

Posted on 2004-08-11
5
Medium Priority
?
2,029 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 1500 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 1500 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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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