Solved

"Handle does not support asynchronous operations"

Posted on 2004-08-11
5
1,958 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

635 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