FileStream Beginread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lou

    FileStream Beginread

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


  • Jon Skeet [C# MVP]

    #2
    Re: FileStream Beginread

    Lou <lou.garvin@com cast.net> wrote:[color=blue]
    > I can't get it to work. Please help...
    > -louie[/color]

    <snikp>
    [color=blue]
    > byte[] byRequest=new byte[4095];
    > iAR=myStream.Be ginRead(byReque st,0,4096,ASync FileCallBackRea d(iAR),myStream )[/color]

    That's a bad start to begin with - you've asked it to read up to 4096
    bytes, but only allocated 4095.
    [color=blue]
    > public void ASyncFileCallBa ckRead(IAsyncRe sult iAR)
    >
    > {
    >
    > System.Text.ASC IIEncoding EnAscii;
    > System.Text.ASC IIEncoding EnUNI;[/color]

    You're declaring these variables, but never setting their values.
    [color=blue]
    > int byteCount;
    >
    > string recData;
    >
    > byte recReq[]=new byte[(Convert.ToByte (iAR.AsyncState )];[/color]

    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 - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Lou

      #3
      Re: FileStream Beginread

      Can you please show me an example.

      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.1a3c39 3328c477bd989bc 5@msnews.micros oft.com...[color=blue]
      > Lou <lou.garvin@com cast.net> wrote:[color=green]
      > > I can't get it to work. Please help...
      > > -louie[/color]
      >
      > <snikp>
      >[color=green]
      > > byte[] byRequest=new byte[4095];
      > >[/color][/color]
      iAR=myStream.Be ginRead(byReque st,0,4096,ASync FileCallBackRea d(iAR),myStream )[color=blue]
      >
      > That's a bad start to begin with - you've asked it to read up to 4096
      > bytes, but only allocated 4095.
      >[color=green]
      > > public void ASyncFileCallBa ckRead(IAsyncRe sult iAR)
      > >
      > > {
      > >
      > > System.Text.ASC IIEncoding EnAscii;
      > > System.Text.ASC IIEncoding EnUNI;[/color]
      >
      > You're declaring these variables, but never setting their values.
      >[color=green]
      > > int byteCount;
      > >
      > > string recData;
      > >
      > > byte recReq[]=new byte[(Convert.ToByte (iAR.AsyncState )];[/color]
      >
      > 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 - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Lou

        #4
        Re: FileStream Beginread

        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]" <skeet@pobox.co m> wrote in message
        news:MPG.1a3c39 3328c477bd989bc 5@msnews.micros oft.com...[color=blue]
        > Lou <lou.garvin@com cast.net> wrote:[color=green]
        > > I can't get it to work. Please help...
        > > -louie[/color]
        >
        > <snikp>
        >[color=green]
        > > byte[] byRequest=new byte[4095];
        > >[/color][/color]
        iAR=myStream.Be ginRead(byReque st,0,4096,ASync FileCallBackRea d(iAR),myStream )[color=blue]
        >
        > That's a bad start to begin with - you've asked it to read up to 4096
        > bytes, but only allocated 4095.
        >[color=green]
        > > public void ASyncFileCallBa ckRead(IAsyncRe sult iAR)
        > >
        > > {
        > >
        > > System.Text.ASC IIEncoding EnAscii;
        > > System.Text.ASC IIEncoding EnUNI;[/color]
        >
        > You're declaring these variables, but never setting their values.
        >[color=green]
        > > int byteCount;
        > >
        > > string recData;
        > >
        > > byte recReq[]=new byte[(Convert.ToByte (iAR.AsyncState )];[/color]
        >
        > 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 - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet
        > If replying to the group, please do not mail me too[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: FileStream Beginread

          Lou <lou.garvin@com cast.net> wrote:[color=blue]
          > I have the code in vb >Net and need to convert it to C#
          > It works great in VB .Net????[/color]

          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 - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Lou

            #6
            Re: FileStream Beginread

            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]" <skeet@pobox.co m> wrote in message
            news:MPG.1a3d6d 91a71431d9989bc b@msnews.micros oft.com...[color=blue]
            > Lou <lou.garvin@com cast.net> wrote:[color=green]
            > > I have the code in vb >Net and need to convert it to C#
            > > It works great in VB .Net????[/color]
            >
            > 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 - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too[/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: FileStream Beginread

              Lou <lou.garvin@com cast.net> wrote:[color=blue]
              > 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);[/color]

              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 - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Lou

                #8
                Re: FileStream Beginread

                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]" <skeet@pobox.co m> wrote in message
                news:MPG.1a3ded 44ddd1bc19989bd 1@msnews.micros oft.com...[color=blue]
                > Lou <lou.garvin@com cast.net> wrote:[color=green]
                > > 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,[/color][/color]
                ASyncFileCallBa ckRead,byReques t);[color=blue]
                >
                > 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 - <skeet@pobox.co m>
                > http://www.pobox.com/~skeet
                > If replying to the group, please do not mail me too[/color]


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: FileStream Beginread

                  Lou <lou.garvin@com cast.net> wrote:[color=blue]
                  > 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"[/color]

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

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

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Lou

                    #10
                    Re: FileStream Beginread

                    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]" <skeet@pobox.co m> wrote in message
                    news:MPG.1a3ead bc3e7162e9989bd 5@msnews.micros oft.com...[color=blue]
                    > Lou <lou.garvin@com cast.net> wrote:[color=green]
                    > > 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[/color][/color]
                    method[color=blue][color=green]
                    > > '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"[/color]
                    >
                    > Ah - sorry, didn't spot that previously. It should be:
                    >
                    > myStream.BeginR ead (byRequest, 0, 4095,
                    > new AsyncCallback (ASyncFileCallB ackRead),
                    > byRequest);
                    >
                    > --
                    > Jon Skeet - <skeet@pobox.co m>
                    > http://www.pobox.com/~skeet
                    > If replying to the group, please do not mail me too[/color]


                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: FileStream Beginread

                      Lou <lou.garvin@com cast.net> wrote:[color=blue]
                      > 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'[/color]

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

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      • Lou

                        #12
                        Re: FileStream Beginread

                        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 ASyncFileCallBa ckRead(IAsyncRe sult iAR)

                        {

                        //byte[] recResponse=iAR .AsyncState;

                        int byteCount;

                        int[] recData;



                        byteCount=myStr eam.EndRead(iAR );

                        recData=iAR.Asy ncState;

                        }

                        "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                        news:MPG.1a3ed2 60ba2f6a1a989bd f@msnews.micros oft.com...[color=blue]
                        > Lou <lou.garvin@com cast.net> wrote:[color=green]
                        > > I still get errors??
                        > >
                        > > C:\Documents and Settings\LGarvi n.PINNACLE\My Documents\Visua l Studio
                        > > Projects\Window sApplication13\ DekoAutomation. cs(154): The best[/color][/color]
                        overloaded[color=blue][color=green]
                        > > 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':[/color][/color]
                        cannot[color=blue][color=green]
                        > > convert from 'WindowsApplica tion13.AsyncCal lback' to[/color][/color]
                        'System.AsyncCa llback'[color=blue]
                        >
                        > Have you declared your own delegate called AsyncCallback? You shouldn't
                        > have done.
                        >
                        > --
                        > Jon Skeet - <skeet@pobox.co m>
                        > http://www.pobox.com/~skeet
                        > If replying to the group, please do not mail me too[/color]


                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: FileStream Beginread

                          Lou <lou.garvin@com cast.net> wrote:[color=blue]
                          > 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??[/color]

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

                          byte[] recResponse = iAR.AsyncState;

                          then use

                          int byteCount = myStream.EndRea d(iAR);

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

                          --
                          Jon Skeet - <skeet@pobox.co m>
                          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                          If replying to the group, please do not mail me too

                          Comment

                          • Lou

                            #14
                            Re: FileStream Beginread

                            the compiler doesn't like'

                            int byteCount = myStream.EndRea d(iAR);

                            C:\Documents and Settings\LGarvi n.PINNACLE\My Documents\Visua l Studio
                            Projects\Window sApplication13\ DekoAutomation. cs(229): Cannot implicitly
                            convert type 'object' to 'byte[]'

                            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                            news:MPG.1a3f08 11e19d3080989be 3@msnews.micros oft.com...[color=blue]
                            > Lou <lou.garvin@com cast.net> wrote:[color=green]
                            > > John I'm making progress. I can now get the call back from the pipe, but[/color][/color]
                            how[color=blue][color=green]
                            > > do I read the data from the pipe??[/color]
                            >
                            > Uncomment the line you commented out, but include a cast:
                            >
                            > byte[] recResponse = iAR.AsyncState;
                            >
                            > then use
                            >
                            > int byteCount = myStream.EndRea d(iAR);
                            >
                            > and then bytes 0 to byteCount-1 are the data that's been read.
                            >
                            > --
                            > Jon Skeet - <skeet@pobox.co m>
                            > http://www.pobox.com/~skeet
                            > If replying to the group, please do not mail me too[/color]


                            Comment

                            • Lou

                              #15
                              Re: FileStream Beginread

                              I DID IT!!

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

                              int byteCount = myStream.EndRea d(iAR);

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




                              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                              news:MPG.1a3f08 11e19d3080989be 3@msnews.micros oft.com...[color=blue]
                              > Lou <lou.garvin@com cast.net> wrote:[color=green]
                              > > John I'm making progress. I can now get the call back from the pipe, but[/color][/color]
                              how[color=blue][color=green]
                              > > do I read the data from the pipe??[/color]
                              >
                              > Uncomment the line you commented out, but include a cast:
                              >
                              > byte[] recResponse = iAR.AsyncState;
                              >
                              > then use
                              >
                              > int byteCount = myStream.EndRea d(iAR);
                              >
                              > and then bytes 0 to byteCount-1 are the data that's been read.
                              >
                              > --
                              > Jon Skeet - <skeet@pobox.co m>
                              > http://www.pobox.com/~skeet
                              > If replying to the group, please do not mail me too[/color]


                              Comment

                              Working...