Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

"Handle does not support asynchronous operations"

Posted on 2004-08-11
5
Medium Priority
?
1,998 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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