本文介绍了FileStream Beginread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我无法上班。请帮忙... -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; 返回false; } //开始asyncronys阅读 IAsyncResult iAR; byte [] byRequest = new byte [4095]; iAR = myStream.BeginRead(byRequest,0,4096,ASyncFileC allBackRead(iAR),myStream) ; public void ASyncFileCallBackRead(IAsyncResult iAR) { 系统.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); 解决方案 Lou< lo ******** @ comcast.net>写道:我不能让它工作。请帮忙... -louie < snikp> byte [] byRequest = new byte [4095]; iAR = myStream.BeginRead(byRequest,0,4096,ASyncFileC allBackRead(iAR),myStream) 这是一个糟糕的开始 - 你已经要求它阅读最多4096 字节,但只分配4095. public void ASyncFileCallBackRead(IAsyncResult iAR) System.Text.ASCIIEncoding EnAscii; System.Text.ASCIIEncoding EnUNI; 你要声明这些变量,但从不设置它们的值。 int byteCount; string recData; 字节recReq [] =新字节[(Convert.ToByte(iAR.AsyncState)]; 为什么要尝试转换iAR.AsyncState到一个字节,你为什么要创建一个新的字节数组?我希望你通过byRequest作为你的状态,而不是 stream(因为你显然在其他地方有一个对流的引用 - 可能是一个好主意,将* both *放在州内,作为一个单独的 type),然后将其强制转换为字节数组。事实上,你完全无视你实际读过的内容。 - Jon Skeet - < sk *** @ pobox.com> http://www.pobox.com/~skeet 如果回复该群组,请不要给我发邮件 你能告诉我一个例子。 " Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道 新闻:MP ************************ @ msnews.microsoft.c om ... Lou< lo ******** @ comcast.net>写道:我不能让它工作。请帮忙...... -louie < snikp> byte [] byRequest = new byte [4095]; iAR = myStream.BeginRead(byRequest,0,4096,ASyncFileC allBackRead(iAR),myStream)这是一个糟糕的开始 - 你已经要求它阅读最多4096 字节,但只分配4095. 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)]; 为什么要尝试将iAR.AsyncState转换为字节,为什么要创建新的字节数组? 我希望你通过byRequest作为你的国家,而不是流(因为你显然在其他地方引用了流 - 它可能是一个好主意,将* both *放在状态中,作为单独的类型),然后只需将其强制转换为字节数组。事实上,你完全无视你实际阅读的内容。 - Jon Skeet - < sk *** @ pobox.com> http://www.pobox.com/~skeet 我的代码在vb> Net并需要将其转换为C# 它在VB .Net中运行良好???? VB代码 ''得到管道 Private Sub btnClient_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnClient.Click pipeHandle = CreateFile(txtInput.Text,DesiredAccess.GENERIC_READ或 DesiredAccess.GENERIC_WRITE,_ ShareMode.FILE_SHARE_READ或ShareMode.FILE_SHARE_WRITE,_ secAttr,CreationDisposition.OPEN_EXISTING,_ FlagsAndAttributes.FILE_FLAG_OVERLAPPED,IntPtr.Zer o) ''确认我们有一个好的处理。 如果(pipeHandle.ToInt32()= INVALID_HANDLE_VALUE)那么 MessageBox.Show(无法打开管道端口, + _ 确保它已安装且未使用。,_ pipePort错误, _ 结束如果 ''根据namedpipe句柄打开一个流。 试试 PS =新FileStream(pipeHandle,FileAccess.ReadWrite,True,4095,True) ''''显示成功消息。 lblPipeHandle.Text = pipeHandle.ToString ''开始异步读取 昏暗byRequest(4095)As Byte DIM iAR As IAsyncResult iAR = PS.BeginRead(byRequest,0,UBound(byRequest)+ 1 ,Address $> 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 ''获取iAR参数传递的数据 Dim recReq()As Byte = CType(iAR.AsyncState,Byte()) byteCount = PS.EndRead(iAR)''获取读取的字节数 如果是byteCount> 0然后 recData = EnASCII.GetString(recReq,0,byteCount) End Sub " Jon Skeet [C# MVP] QUOT; < SK *** @ pobox.com>在消息中写道 新闻:MP ************************ @ msnews.microsoft.c om ... Lou< lo ******** @ comcast.net>写道:我不能让它工作。请帮忙...... -louie < snikp> byte [] byRequest = new byte [4095]; iAR = myStream.BeginRead(byRequest,0,4096,ASyncFileC allBackRead(iAR),myStream)这是一个糟糕的开始 - 你已经要求它阅读最多4096 字节,但只分配4095. 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)]; 为什么要尝试将iAR.AsyncState转换为字节,为什么要创建新的字节数组? 我希望你通过byRequest作为你的国家,而不是流(因为你显然在其他地方引用了流 - 它可能是一个好主意,将* both *放在状态中,作为单独的类型),然后只需将其强制转换为字节数组。事实上,你完全无视你实际阅读的内容。 - Jon Skeet - < sk *** @ pobox.com> http://www.pobox.com/~skeet 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,ASyncFileC allBackRead(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); 解决方案 Lou <lo********@comcast.net> wrote: I can''t get it to work. Please help... -louie<snikp> byte[] byRequest=new byte[4095]; iAR=myStream.BeginRead(byRequest,0,4096,ASyncFileC allBackRead(iAR),myStream)That''s a bad start to begin with - you''ve asked it to read up to 4096bytes, 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 youcreating a new byte array? I would expect you to pass byRequest as your state, rather than thestream (as you apparently have a reference to the stream elsewhere - itmight well be a good idea to put *both* in the state, as a separatetype), and then just cast it back to a byte array. As it is, you''recompletely ignoring whatever you''ve actually read. --Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me tooCan you please show me an example. "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in messagenews:MP************************@msnews.microsoft.c om... Lou <lo********@comcast.net> wrote: I can''t get it to work. Please help... -louie <snikp> byte[] byRequest=new byte[4095];iAR=myStream.BeginRead(byRequest,0,4096,ASyncFileC allBackRead(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. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me tooI 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 AsSystem.EventArgs) Handles btnClient.Click pipeHandle = CreateFile(txtInput.Text, DesiredAccess.GENERIC_READ OrDesiredAccess.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, AddressOfASyncFileCallBackRead, 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 [C# MVP]" <sk***@pobox.com> wrote in messagenews:MP************************@msnews.microsoft.c om... Lou <lo********@comcast.net> wrote: I can''t get it to work. Please help... -louie <snikp> byte[] byRequest=new byte[4095];iAR=myStream.BeginRead(byRequest,0,4096,ASyncFileC allBackRead(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. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too 这篇关于FileStream Beginread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 12:54