473,765 Members | 1,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FileStream Beginread

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

hPipe = CreateFile(conn ectionString,

GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRIT E, 0,

OPEN_EXISTING, FILE_ATTRIBUTE_ NORMAL,HANDLE.Z ero);

if ((hPipe.ToInt32 ()) == INVALID_HANDLE_ VALUE)

{

blnConnected=fa lse;

return false;

}

//start an asyncronys read

IAsyncResult iAR;

byte[] byRequest=new byte[4095];

iAR=myStream.Be ginRead(byReque st,0,4096,ASync FileCallBackRea d(iAR),myStream )
;

public void ASyncFileCallBa ckRead(IAsyncRe sult iAR)

{

System.Text.ASC IIEncoding EnAscii;

System.Text.ASC IIEncoding EnUNI;

int byteCount;

string recData;

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

byteCount=myStr eam.EndRead(iAR );
recData = EnAscii.GetStri ng(recReq,0,byt eCount);
Nov 15 '05 #1
15 10820
Lou <lo********@com cast.net> wrote:
I can't get it to work. Please help...
-louie
<snikp>
byte[] byRequest=new byte[4095];
iAR=myStream.Be ginRead(byReque st,0,4096,ASync FileCallBackRea d(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 ASyncFileCallBa ckRead(IAsyncRe sult iAR)

{

System.Text.ASC IIEncoding EnAscii;
System.Text.ASC IIEncoding 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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Lou
Can you please show me an example.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Lou <lo********@com cast.net> wrote:
I can't get it to work. Please help...
-louie
<snikp>
byte[] byRequest=new byte[4095];

iAR=myStream.Be ginRead(byReque st,0,4096,ASync FileCallBackRea d(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 ASyncFileCallBa ckRead(IAsyncRe sult iAR)

{

System.Text.ASC IIEncoding EnAscii;
System.Text.ASC IIEncoding 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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
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.EventArg s) Handles btnClient.Click

pipeHandle = CreateFile(txtI nput.Text, DesiredAccess.G ENERIC_READ Or
DesiredAccess.G ENERIC_WRITE, _

ShareMode.FILE_ SHARE_READ Or ShareMode.FILE_ SHARE_WRITE, _

secAttr, CreationDisposi tion.OPEN_EXIST ING, _

FlagsAndAttribu tes.FILE_FLAG_O VERLAPPED, IntPtr.Zero)

' Verify we have a good handle.

If (pipeHandle.ToI nt32() = 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(pipe Handle, FileAccess.Read Write, True, 4095, True)

'' Display a success message.

lblPipeHandle.T ext = pipeHandle.ToSt ring

'start async read

Dim byRequest(4095) As Byte

Dim iAR As IAsyncResult

iAR = PS.BeginRead(by Request, 0, UBound(byReques t) + 1, AddressOf
ASyncFileCallBa ckRead, byRequest)

End Sub

Private Sub ASyncFileCallBa ckRead(ByVal iAR As IAsyncResult)

Dim EnASCII As New System.Text.ASC IIEncoding()

Dim EnUNI As New System.Text.Uni codeEncoding()

Dim byteCount As Integer

Dim recData As String

'get the data passed in parameter of iAR

Dim recReq() As Byte = CType(iAR.Async State, Byte())

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

If byteCount > 0 Then

recData = EnASCII.GetStri ng(recReq, 0, byteCount)
End Sub
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Lou <lo********@com cast.net> wrote:
I can't get it to work. Please help...
-louie
<snikp>
byte[] byRequest=new byte[4095];

iAR=myStream.Be ginRead(byReque st,0,4096,ASync FileCallBackRea d(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 ASyncFileCallBa ckRead(IAsyncRe sult iAR)

{

System.Text.ASC IIEncoding EnAscii;
System.Text.ASC IIEncoding 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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
Lou <lo********@com cast.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Lou
So why doesn't this work, I'm very confused...

IAsyncResult iAR;

byte[] byRequest=new byte[4095];

iAR=myStream.Be ginRead(byReque st,0,4095, ASyncFileCallBa ckRead,byReques t);

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Lou <lo********@com cast.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6
Lou <lo********@com cast.net> wrote:
So why doesn't this work, I'm very confused...

IAsyncResult iAR;

byte[] byRequest=new byte[4095];

iAR=myStream.Be ginRead(byReque st,0,4095, ASyncFileCallBa ckRead,byReques t);


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.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
Lou
First off thank you for being patient as I am new to C#..

here is the erroe message,

C:\Documents and Settings\LGarvi n.PINNACLE\My Documents\Visua l Studio
Projects\Window sApplication13\ DekoAutomation. cs(160): No overload for method
'ASyncFileCallB ackRead' takes '0' arguments

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

Thanks..
-Lou

//start an asyncronys read

//System.AsyncCal lback

IAsyncResult iAR;
byte[] byRequest=new byte[4095];

iAR=myStream.Be ginRead(byReque st,0,4095,ASync FileCallBackRea d(),byRequest);

public void ASyncFileCallBa ckRead(IAsyncRe sult iAR)

{

/*

System.Text.ASC IIEncoding EnAscii;

System.Text.ASC IIEncoding EnUNI;

int byteCount;

string recData;

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

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

byteCount=myStr eam.EndRead(iAR );
recData = EnAscii.GetStri ng(recReq,0,byt eCount);

*/



}

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Lou <lo********@com cast.net> wrote:
So why doesn't this work, I'm very confused...

IAsyncResult iAR;

byte[] byRequest=new byte[4095];

iAR=myStream.Be ginRead(byReque st,0,4095,
ASyncFileCallBa ckRead,byReques t);
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.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8
Lou <lo********@com cast.net> wrote:
First off thank you for being patient as I am new to C#..

here is the erroe message,

C:\Documents and Settings\LGarvi n.PINNACLE\My Documents\Visua l Studio
Projects\Window sApplication13\ DekoAutomation. cs(160): No overload for method
'ASyncFileCallB ackRead' takes '0' arguments

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


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

myStream.BeginR ead (byRequest, 0, 4095,
new AsyncCallback (ASyncFileCallB ackRead),
byRequest);

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
Lou
I still get errors??

C:\Documents and Settings\LGarvi n.PINNACLE\My Documents\Visua l Studio
Projects\Window sApplication13\ DekoAutomation. cs(154): The best overloaded
method match for 'System.IO.Stre am.BeginRead(by te[], int, int,
System.AsyncCal lback, object)' has some invalid arguments
C:\Documents and Settings\LGarvi n.PINNACLE\My Documents\Visua l Studio
Projects\Window sApplication13\ DekoAutomation. cs(155): Argument '4': cannot
convert from 'WindowsApplica tion13.AsyncCal lback' to 'System.AsyncCa llback'

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Lou <lo********@com cast.net> wrote:
First off thank you for being patient as I am new to C#..

here is the erroe message,

C:\Documents and Settings\LGarvi n.PINNACLE\My Documents\Visua l Studio
Projects\Window sApplication13\ DekoAutomation. cs(160): No overload for method 'ASyncFileCallB ackRead' takes '0' arguments

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


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

myStream.BeginR ead (byRequest, 0, 4095,
new AsyncCallback (ASyncFileCallB ackRead),
byRequest);

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1830
by: Martyn Wynne | last post by:
Hi, Can anyone please tell me if there is any reason why when i am streaming from a webrequest (decompressing on route) to a file on the hard drive, i would be getting an exception of Filestream error System.StackOverflowException every time the file gets to 8580Kbs. Code is basically a modified Microsoft example, it errors on the line Dim ar As IAsyncResult = _
0
1271
by: Shane | last post by:
I want to use BeginRead on a socket stream. I want to abort the read if no data arrives within a certain time interval. I know i can't cancell the BeginRead directly but closing the socket seems to work. Does this cause any problems (Resource leaks etc) ? Should I also call EndRead after closing the socket even though it will it throw ? Has anyone done anything similar ? Shane
4
7206
by: 0to60 | last post by:
I have a class that wraps a TcpClient object and manages all the async reading of the socket. It works really nice, and I use it all over the place. But there's this ONE INSTANCE where I create one of these things and it WON'T read data. If I set a breakpoint in my EndRead callback, it never goes off. NOTHING is different from anywhere else I use this class, its just this one place. Now, if I create a second constructor for my class...
5
6421
by: Mike Robinson | last post by:
The Win32 SDK had a great function called "ReadFile" that was a model of simplicity. Now I'm starting to use the new .NET Framework, which is supposed to be an improvement but the file reading is (as far as I can tell) nowhere near as good. FileStream fs=new FileStream(FName); fs.BeginRead (Buffer,0,thisFile.Length,0,0); The last two arguments (the two closing zeroes) are for "AsyncCallback" and "stateObject." Microsoft's...
9
5855
by: Tom | last post by:
I am working with the this object as oppose to the StreamReader object becuase I need to access a file (to find the contents) while an external application is updating the file. When I was working with the StreamReader object I got a deadlock when I tried reading the file while my external application was writing to it. So far I am able to create a FileStream object and open the file in question (CrawlerBackup.txt). But I am unable to...
1
2951
by: Manuel Costa | last post by:
Hi, i was testing a network application that i've been working on which use .net socket components. To read from a socket i use the networkstream beginread. If the other side dies, beginread calls the callback delegate. Today i was testing this in my old laptop (celeron 553MHz) and verify an interesting thing. Callback is not called and this exception is captured by visual studio. An unhandled exception of type...
5
3691
by: Nadav | last post by:
Hi, I am using FileStream's Async API: BeginRead/EndRead, upon completion callback execution I use the read data and call EndRead, Taking that in mind, I Wonder... does calling EndRead will cause a context switch? What is the kernel object used for blocking EndRead calls? Event and mutex cause a context switch even when the object is signaled and no wait is needed, usage of critical section prevent this switch from happening... what is the...
6
5763
by: Stephen Brooker | last post by:
Hi all, I've got a basic TCP app that is giving me trouble. I have a separate class that takes care of the TCP connection, and uses the NetworkStreams BeginRead and EndRead with a callback function to deal with the server response. All works well and the data is received OK. Once all the data is received, the connection is closed and I fire an event indicating all is finished. The event is handled in the main form of the application and...
1
5332
by: ohmmega | last post by:
hello, i've a method in the form_load event witch calls 2 beginread statements (one after the other :) ) problem: during the second call it seem's that the callback won't be triggered. i've tried to wait for several seconds before calling the second call - no effect.
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.