Solved

"Handle does not support asynchronous operations"

Posted on 2004-08-11
5
1,840 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now