FileStream Beginread

L

Lou

I can't get it to work. Please help...
-louie

hPipe = CreateFile(connectionString,

GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,

OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,HANDLE.Zero);

if ((hPipe.ToInt32()) == INVALID_HANDLE_VALUE)

{

blnConnected=false;

return false;

}

//start an asyncronys read

IAsyncResult iAR;

byte[] byRequest=new byte[4095];

iAR=myStream.BeginRead(byRequest,0,4096,ASyncFileCallBackRead(iAR),myStream)
;



public void ASyncFileCallBackRead(IAsyncResult iAR)

{

System.Text.ASCIIEncoding EnAscii;

System.Text.ASCIIEncoding EnUNI;

int byteCount;

string recData;

byte recReq[]=new byte[(Convert.ToByte(iAR.AsyncState)];

byteCount=myStream.EndRead(iAR);


recData = EnAscii.GetString(recReq,0,byteCount);
 
J

Jon Skeet [C# MVP]

Lou said:
I can't get it to work. Please help...
-louie

byte[] byRequest=new byte[4095];
iAR=myStream.BeginRead(byRequest,0,4096,ASyncFileCallBackRead(iAR),myStream)

That's a bad start to begin with - you've asked it to read up to 4096
bytes, but only allocated 4095.
public void ASyncFileCallBackRead(IAsyncResult iAR)

{

System.Text.ASCIIEncoding EnAscii;
System.Text.ASCIIEncoding EnUNI;

You're declaring these variables, but never setting their values.
int byteCount;

string recData;

byte recReq[]=new byte[(Convert.ToByte(iAR.AsyncState)];

Why are you trying to convert iAR.AsyncState to a byte, and why are you
creating a new byte array?

I would expect you to pass byRequest as your state, rather than the
stream (as you apparently have a reference to the stream elsewhere - it
might well be a good idea to put *both* in the state, as a separate
type), and then just cast it back to a byte array. As it is, you're
completely ignoring whatever you've actually read.
 
L

Lou

Can you please show me an example.

Jon Skeet said:
Lou said:
I can't get it to work. Please help...
-louie

byte[] byRequest=new byte[4095];
iAR=myStream.BeginRead(byRequest,0,4096,ASyncFileCallBackRead(iAR),myStream)

That's a bad start to begin with - you've asked it to read up to 4096
bytes, but only allocated 4095.
public void ASyncFileCallBackRead(IAsyncResult iAR)

{

System.Text.ASCIIEncoding EnAscii;
System.Text.ASCIIEncoding EnUNI;

You're declaring these variables, but never setting their values.
int byteCount;

string recData;

byte recReq[]=new byte[(Convert.ToByte(iAR.AsyncState)];

Why are you trying to convert iAR.AsyncState to a byte, and why are you
creating a new byte array?

I would expect you to pass byRequest as your state, rather than the
stream (as you apparently have a reference to the stream elsewhere - it
might well be a good idea to put *both* in the state, as a separate
type), and then just cast it back to a byte array. As it is, you're
completely ignoring whatever you've actually read.
 
L

Lou

I have the code in vb >Net and need to convert it to C#
It works great in VB .Net????
VB Code

'get handle to the pipe

Private Sub btnClient_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClient.Click

pipeHandle = CreateFile(txtInput.Text, DesiredAccess.GENERIC_READ Or
DesiredAccess.GENERIC_WRITE, _

ShareMode.FILE_SHARE_READ Or ShareMode.FILE_SHARE_WRITE, _

secAttr, CreationDisposition.OPEN_EXISTING, _

FlagsAndAttributes.FILE_FLAG_OVERLAPPED, IntPtr.Zero)

' Verify we have a good handle.

If (pipeHandle.ToInt32() = INVALID_HANDLE_VALUE) Then

MessageBox.Show("Can't open the pipe port, " + _

"make sure it's installed and not in use.", _

"pipePort Error", _

End If

' Open a stream based on the namedpipe handle.

Try

PS = New FileStream(pipeHandle, FileAccess.ReadWrite, True, 4095, True)

'' Display a success message.

lblPipeHandle.Text = pipeHandle.ToString

'start async read

Dim byRequest(4095) As Byte

Dim iAR As IAsyncResult

iAR = PS.BeginRead(byRequest, 0, UBound(byRequest) + 1, AddressOf
ASyncFileCallBackRead, byRequest)

End Sub

Private Sub ASyncFileCallBackRead(ByVal iAR As IAsyncResult)

Dim EnASCII As New System.Text.ASCIIEncoding()

Dim EnUNI As New System.Text.UnicodeEncoding()

Dim byteCount As Integer

Dim recData As String

'get the data passed in parameter of iAR

Dim recReq() As Byte = CType(iAR.AsyncState, Byte())

byteCount = PS.EndRead(iAR) 'get number of bytes read

If byteCount > 0 Then

recData = EnASCII.GetString(recReq, 0, byteCount)


End Sub


Jon Skeet said:
Lou said:
I can't get it to work. Please help...
-louie

byte[] byRequest=new byte[4095];
iAR=myStream.BeginRead(byRequest,0,4096,ASyncFileCallBackRead(iAR),myStream)

That's a bad start to begin with - you've asked it to read up to 4096
bytes, but only allocated 4095.
public void ASyncFileCallBackRead(IAsyncResult iAR)

{

System.Text.ASCIIEncoding EnAscii;
System.Text.ASCIIEncoding EnUNI;

You're declaring these variables, but never setting their values.
int byteCount;

string recData;

byte recReq[]=new byte[(Convert.ToByte(iAR.AsyncState)];

Why are you trying to convert iAR.AsyncState to a byte, and why are you
creating a new byte array?

I would expect you to pass byRequest as your state, rather than the
stream (as you apparently have a reference to the stream elsewhere - it
might well be a good idea to put *both* in the state, as a separate
type), and then just cast it back to a byte array. As it is, you're
completely ignoring whatever you've actually read.
 
J

Jon Skeet [C# MVP]

Lou said:
I have the code in vb >Net and need to convert it to C#
It works great in VB .Net????

Well, it looks like the VB code *mostly* has the changes I was talking
about. I'd suggest just using the Encoding.ASCII property to get an
ASCIIEncoding instance rather than creating a new one, but the salient
points are:

o byRequest is passed instead of myStream as the parameter
o It's casting AsyncState to a byte array, not creating a *new* one
using Convert.ToByte as the length

Note that the VB line "Dim byRequest(4095) As Byte" is equivalent to
the C#

byte[] byRequest = new byte[4096]; as far as I know.
 
L

Lou

So why doesn't this work, I'm very confused...

IAsyncResult iAR;

byte[] byRequest=new byte[4095];

iAR=myStream.BeginRead(byRequest,0,4095, ASyncFileCallBackRead,byRequest);

Jon Skeet said:
Lou said:
I have the code in vb >Net and need to convert it to C#
It works great in VB .Net????

Well, it looks like the VB code *mostly* has the changes I was talking
about. I'd suggest just using the Encoding.ASCII property to get an
ASCIIEncoding instance rather than creating a new one, but the salient
points are:

o byRequest is passed instead of myStream as the parameter
o It's casting AsyncState to a byte array, not creating a *new* one
using Convert.ToByte as the length

Note that the VB line "Dim byRequest(4095) As Byte" is equivalent to
the C#

byte[] byRequest = new byte[4096]; as far as I know.
 
J

Jon Skeet [C# MVP]

Lou said:
So why doesn't this work, I'm very confused...

IAsyncResult iAR;

byte[] byRequest=new byte[4095];

iAR=myStream.BeginRead(byRequest,0,4095, ASyncFileCallBackRead,byRequest);

That should work fine - what makes you think it doesn't?

(Note that unless you're actually going to use the returned
IAsyncResult, you don't need to store it in a variable anywhere.)
 
L

Lou

First off thank you for being patient as I am new to C#..

here is the erroe message,

C:\Documents and Settings\LGarvin.PINNACLE\My Documents\Visual Studio
Projects\WindowsApplication13\DekoAutomation.cs(160): No overload for method
'ASyncFileCallBackRead' takes '0' arguments

Am I supposed to use a delegate(which are new to me) as a parameter
for the "myStream.BeginRead" to point to my callback function
"AsyncFileCallBackread"

Thanks..
-Lou



//start an asyncronys read

//System.AsyncCallback

IAsyncResult iAR;


byte[] byRequest=new byte[4095];

iAR=myStream.BeginRead(byRequest,0,4095,ASyncFileCallBackRead(),byRequest);

public void ASyncFileCallBackRead(IAsyncResult iAR)

{

/*

System.Text.ASCIIEncoding EnAscii;

System.Text.ASCIIEncoding EnUNI;

int byteCount;

string recData;

//byte recReq[];


//byte[] byRequest=new byte[4095];

byte recReq[]=new byte[(Convert.ToByte(iAR.AsyncState)];

byteCount=myStream.EndRead(iAR);


recData = EnAscii.GetString(recReq,0,byteCount);

*/









}

Jon Skeet said:
Lou said:
So why doesn't this work, I'm very confused...

IAsyncResult iAR;

byte[] byRequest=new byte[4095];

iAR=myStream.BeginRead(byRequest,0,4095,
ASyncFileCallBackRead,byRequest);

That should work fine - what makes you think it doesn't?

(Note that unless you're actually going to use the returned
IAsyncResult, you don't need to store it in a variable anywhere.)
 
J

Jon Skeet [C# MVP]

Lou said:
First off thank you for being patient as I am new to C#..

here is the erroe message,

C:\Documents and Settings\LGarvin.PINNACLE\My Documents\Visual Studio
Projects\WindowsApplication13\DekoAutomation.cs(160): No overload for method
'ASyncFileCallBackRead' takes '0' arguments

Am I supposed to use a delegate(which are new to me) as a parameter
for the "myStream.BeginRead" to point to my callback function
"AsyncFileCallBackread"

Ah - sorry, didn't spot that previously. It should be:

myStream.BeginRead (byRequest, 0, 4095,
new AsyncCallback (ASyncFileCallBackRead),
byRequest);
 
L

Lou

I still get errors??

C:\Documents and Settings\LGarvin.PINNACLE\My Documents\Visual Studio
Projects\WindowsApplication13\DekoAutomation.cs(154): The best overloaded
method match for 'System.IO.Stream.BeginRead(byte[], int, int,
System.AsyncCallback, object)' has some invalid arguments


C:\Documents and Settings\LGarvin.PINNACLE\My Documents\Visual Studio
Projects\WindowsApplication13\DekoAutomation.cs(155): Argument '4': cannot
convert from 'WindowsApplication13.AsyncCallback' to 'System.AsyncCallback'
 
J

Jon Skeet [C# MVP]

Lou said:
I still get errors??

C:\Documents and Settings\LGarvin.PINNACLE\My Documents\Visual Studio
Projects\WindowsApplication13\DekoAutomation.cs(154): The best overloaded
method match for 'System.IO.Stream.BeginRead(byte[], int, int,
System.AsyncCallback, object)' has some invalid arguments


C:\Documents and Settings\LGarvin.PINNACLE\My Documents\Visual Studio
Projects\WindowsApplication13\DekoAutomation.cs(155): Argument '4': cannot
convert from 'WindowsApplication13.AsyncCallback' to 'System.AsyncCallback'

Have you declared your own delegate called AsyncCallback? You shouldn't
have done.
 
L

Lou

John I'm making progress. I can now get the call back from the pipe, but how
do I read the data from the pipe??
Thanks again for all your help/patience.
-Lou

public void ASyncFileCallBackRead(IAsyncResult iAR)

{

//byte[] recResponse=iAR.AsyncState;

int byteCount;

int[] recData;



byteCount=myStream.EndRead(iAR);

recData=iAR.AsyncState;

}

Jon Skeet said:
Lou said:
I still get errors??

C:\Documents and Settings\LGarvin.PINNACLE\My Documents\Visual Studio
Projects\WindowsApplication13\DekoAutomation.cs(154): The best overloaded
method match for 'System.IO.Stream.BeginRead(byte[], int, int,
System.AsyncCallback, object)' has some invalid arguments


C:\Documents and Settings\LGarvin.PINNACLE\My Documents\Visual Studio
Projects\WindowsApplication13\DekoAutomation.cs(155): Argument '4': cannot
convert from 'WindowsApplication13.AsyncCallback' to
'System.AsyncCallback'

Have you declared your own delegate called AsyncCallback? You shouldn't
have done.
 
J

Jon Skeet [C# MVP]

Lou said:
John I'm making progress. I can now get the call back from the pipe, but how
do I read the data from the pipe??

Uncomment the line you commented out, but include a cast:

byte[] recResponse = iAR.AsyncState;

then use

int byteCount = myStream.EndRead(iAR);

and then bytes 0 to byteCount-1 are the data that's been read.
 
L

Lou

the compiler doesn't like'

int byteCount = myStream.EndRead(iAR);

C:\Documents and Settings\LGarvin.PINNACLE\My Documents\Visual Studio
Projects\WindowsApplication13\DekoAutomation.cs(229): Cannot implicitly
convert type 'object' to 'byte[]'

Jon Skeet said:
Lou said:
John I'm making progress. I can now get the call back from the pipe, but how
do I read the data from the pipe??

Uncomment the line you commented out, but include a cast:

byte[] recResponse = iAR.AsyncState;

then use

int byteCount = myStream.EndRead(iAR);

and then bytes 0 to byteCount-1 are the data that's been read.
 
L

Lou

I DID IT!!

byte[] recResponse=(byte[])iAR.AsyncState;

int byteCount = myStream.EndRead(iAR);

thanks for your support, your a patient person and its appreciated.




Jon Skeet said:
Lou said:
John I'm making progress. I can now get the call back from the pipe, but how
do I read the data from the pipe??

Uncomment the line you commented out, but include a cast:

byte[] recResponse = iAR.AsyncState;

then use

int byteCount = myStream.EndRead(iAR);

and then bytes 0 to byteCount-1 are the data that's been read.
 
J

Jon Skeet [C# MVP]

Lou said:
the compiler doesn't like'

int byteCount = myStream.EndRead(iAR);

C:\Documents and Settings\LGarvin.PINNACLE\My Documents\Visual Studio
Projects\WindowsApplication13\DekoAutomation.cs(229): Cannot implicitly
convert type 'object' to 'byte[]'

I don't think that's the line it's complaining about. I think it's
complaining because you've done:

byte[] byRequest = iAR.AsyncState;

where you should have

byte[] byRequest = (byte[]) iAR.AsyncState;

(My fault for not putting it before.)

I think you could do with learning a bit more about the basics of C#
before going too much further with asynchronous stuff though - it would
help you solve a lot of your problems yourself.
 
Top