Ejemplo n.º 1
0
 protected override void InitializeProtected()
 {
     if (_stream == null)
     {
         _stream = new AsyncFileStream(_fileName, FileShare.ReadWrite);
     }
 }
Ejemplo n.º 2
0
        private static void WithReadableStream(string path, Func <AsyncFileStream, Task> action)
        {
#if NET_FRAMEWORK
            using (var io = new IOCompletionManager())
#else
            IIOCompletionManager io = null;
#endif
            {
                using (
                    IAsyncFile file = AsyncFileFactory.CreateOrOpen(
                        path,
                        FileDesiredAccess.GenericRead,
                        FileShare.Read | FileShare.Delete,
                        FileMode.Open,
                        FileFlagsAndAttributes.None,
                        io))
                {
                    using (AsyncFileStream stream = file.CreateReadableStream())
                    {
                        XAssert.IsTrue(stream.CanRead);
                        XAssert.IsTrue(stream.CanSeek);
                        XAssert.IsFalse(stream.CanWrite);

                        action(stream).GetAwaiter().GetResult();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private static async Task Read(AsyncFileStream stream, byte[] buffer, int offset, int count)
        {
            int remaining = count;

            while (remaining > 0)
            {
                int read = await stream.ReadAsync(buffer, offset, remaining);

                XAssert.AreNotEqual(0, read, "Unexpected EOF");
                remaining -= read;
                offset    += read;
            }
        }
        public async Task <BitmapImage> ReadImageAsync(string path)
        {
            logger.Info("Do ReadImageAsync:" + path);
            using (AsyncFileStream afs = new AsyncFileStream(path, FileMode.Open, FileAccess.Read))
            {
                byte[] buffer = new byte[afs.Length];
                await afs.ReadAsync(buffer, 0, (int)afs.Length); // 图片不能超过4G

                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = new MemoryStream(buffer);
                bitmapImage.EndInit();
                return(bitmapImage);
            }
        }
Ejemplo n.º 5
0
        private void RegisterNamedPipePort(NamedPipeConnectData connectData)
        {
            VerifyAccess();

            AsyncFileStream stream = new AsyncFileStream(connectData._pipeName, connectData._pipe);

            ComPortToNamedPipe namedPipePort = new ComPortToNamedPipe(connectData._pipeName, stream);

            namedPipePort.ComPortHandle = connectData._comPortHandle;

            this.Emulator.RegisterComponent(namedPipePort);

            connectData._isRegistered = true;

            //The TinyCLR doesn't call Initialize for the debug NamedPipe ports always
            //and we are swapping out components anyway, so we need to force the
            //initialization here to start the reading
            namedPipePort.DeviceInitialize();
        }
Ejemplo n.º 6
0
        private void RegisterNamedPipePort(NamedPipeConnectData connectData)
        {
            VerifyAccess();

            AsyncFileStream stream = new AsyncFileStream(connectData._pipeName, connectData._pipe);

            ComPortToNamedPipe namedPipePort = new ComPortToNamedPipe(connectData._pipeName, stream);

            namedPipePort.ComPortHandle = connectData._comPortHandle;

            this.Emulator.RegisterComponent(namedPipePort);

            connectData._isRegistered = true;

            //The TinyCLR doesn't call Initialize for the debug NamedPipe ports always
            //and we are swapping out components anyway, so we need to force the
            //initialization here to start the reading
            namedPipePort.DeviceInitialize();
        }