简体   繁体   English

System.ObjectDisposedException UWP

[英]System.ObjectDisposedException UWP

i've an issue in my application. 我的应用程序有问题。 I'm trying to send an image into FTP server. 我正在尝试将图像发送到FTP服务器。 I'm able to connect with server, authenticate, and other. 我能够与服务器连接,进行身份验证等。 But when i try to send file, if the file is big (usually bigger than 40-50kb) i got System.ObjectDisposedException. 但是当我尝试发送文件时,如果文件很大(通常大于40-50kb),则会收到System.ObjectDisposedException。

This is the code that i use to send: 这是我用来发送的代码:

public async Task <byte[]> GetResultingBuffer(IRandomAccessStreamWithContentType readStream, IBuffer buffer)
        {
            var resultingBuffer = new byte[0];
            while (true)
            {
                IBuffer readBuffer = await readStream.ReadAsync(buffer, 1024, InputStreamOptions.Partial);

                if (readBuffer.Length == 0) break;

                resultingBuffer = resultingBuffer.Concat(readBuffer.ToArray()).ToArray();
            }
            return resultingBuffer;
        }

        public async Task UploadFileAsync(StorageFile file, string destination)
        {
            using (var stream = await OpenWriteAsync(destination))
            {
                //
                // A more efficient way, maybe a DataReader can be used here
                using (var readStream = await file.OpenReadAsync())
                {
                    var buffer = new byte[1024].AsBuffer();
                    var resultingBuffer = new byte[0];
                    resultingBuffer = await GetResultingBuffer(readStream, buffer);


                    await stream.WriteAsync(resultingBuffer.AsBuffer());
                    await stream.FlushAsync();
                }
            }
        }

I tried to edit it, before editing GetResultingBuffer was not a task, but a cycle inside UploadFileAsnc. 在编辑GetResultingBuffer之前,我尝试对其进行编辑,这不是一个任务,而是UploadFileAsnc内部的一个循环。 How can i prevent to dispose the buffer? 我如何防止处置缓冲区? Is there another solution? 还有其他解决方案吗? Thanks! 谢谢!

I solved it in this way 我以这种方式解决了

using (var readStream = await file.OpenReadAsync())
                {
                    var buffer = new byte[3000000].AsBuffer();
                    //var resultingBuffer = new byte[10000000];
                    Debug.Write("-------");
                    //while (true)
                    //{
                        IBuffer readBuffer = await readStream.ReadAsync(buffer, 3000000, InputStreamOptions.Partial);

                    //if (readBuffer.Length == 0) break;

                    //resultingBuffer = resultingBuffer.Concat(readBuffer.ToArray()).ToArray();
                    //}

                    // await stream.WriteAsync(resultingBuffer.AsBuffer());
                    var  resultingBuffer = new byte[readBuffer.Length];
                    readBuffer.CopyTo(resultingBuffer);

                    await stream.WriteAsync(resultingBuffer.AsBuffer());
                }

                Debug.Write("-------");
                await stream.FlushAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM