private void readAsyncHandler(IAsyncResult ar) { if (InvokeRequired) { // We'll update GUI, so we need to pass the async call back processing // to the GUI thread. BeginInvoke(new AsyncCallback(readAsyncHandler), new object[] { ar }); return; } PLC p = (PLC)ar.AsyncState; BinaryReader br; try { br = p.EndRead(ar); rwStatusL.Text = "Done"; rwHB.ByteProvider = new DynamicByteProvider(new byte[br.BaseStream.Length]); for (int i = 0; i < br.BaseStream.Length; i++) { rwHB.ByteProvider.WriteByte(i, br.ReadByte()); } } catch (IndexOutOfRangeException exc) { rwStatusL.Text = "Out of range or max request capacity"; } catch (Exception exc) { rwStatusL.Text = "Unexpected error: " + exc.Message; } }
public void ReadWriteRawAsyncTest() { const int INT_COUNT = 200; MemoryStream ms = new MemoryStream(INT_COUNT * 4); BinaryWriter bw = new BinaryWriter(ms); for (int i = 0; i < INT_COUNT; i++) { bw.Write(i * 2); } IAsyncResult ar = plc.BeginWriteRaw(0, ms.GetBuffer(), null, null); ar.AsyncWaitHandle.WaitOne(); plc.EndWrite(ar); ar = plc.BeginRead(0, INT_COUNT * 4, null, null); ar.AsyncWaitHandle.WaitOne(); BinaryReader br = plc.EndRead(ar); for (int i = 0; i < INT_COUNT; i++) { Assert.AreEqual(i * 2, br.ReadInt32()); } }