简体   繁体   English

在多个线程中将Stream读取到MemoryStream

[英]Reading Stream to a MemoryStream in multiple threads

I am stuck up in a place. 我被困在一个地方。 I am reading a flv file from a URL. 我正在从URL读取flv文件。 I am reading this to a Stream and then writing this Stream to a MemoryStream in a loop. 我正在将其读取到Stream,然后将此Stream写入循环中的MemoryStream。 When the code comes out of the loop, I am writing the whole MemoryStream to a ByteArray and then writing this ByteArray to a local file on my hard disk. 当代码从循环中出来时,我将整个MemoryStream写入ByteArray,然后将此ByteArray写入硬盘上的本地文件。

As this flv is too large, it takes a lot of time to process in the loop. 由于此flv太大,因此在循环中处理需要花费大量时间。 I am thinking of reading the original large stream in MemoryStream in multiple threads. 我正在考虑在多个线程中读取MemoryStream中的原始大流。 That means dividing the Stream in say 10 parts and writing these parts to MemoryStream in multiple threads. 这意味着将Stream分成10个部分,并将这些部分写入多个线程中的MemoryStream。 How do I do this? 我该怎么做呢?

I am attaching my piece of code. 我附上了我的代码。

//Get a data stream from the url
                WebRequest req = WebRequest.Create(url);
                WebResponse response = req.GetResponse();
                using (Stream stream = response.GetResponseStream())
                {
                    //Download in chuncks
                    byte[] buffer = new byte[1024];

                    //Get Total Size
                    int dataLength = (int)response.ContentLength;



                    //Download to memory
                    //Note: adjust the streams here to download directly to the hard drive
                    using (MemoryStream memStream = new MemoryStream())
                    {
                        while (true)
                        {
                            //Try to read the data
                            int bytesRead = stream.Read(buffer, 0, buffer.Length);

                            if (bytesRead == 0)
                            {
                                Application.DoEvents();
                                break;
                            }
                            else
                            {
                                //Write the downloaded data
                                memStream.Write(buffer, 0, bytesRead);
                            }
                        }

                        //Convert the downloaded stream to a byte array
                        byte[] downloadedData = memStream.ToArray();
                    }  


                }

Any help is appreciated Thanks 任何帮助表示赞赏谢谢

You won't be able to speed up the download by using multiple threads. 您将无法使用多个线程加快下载速度。 The limiting factor here is not how fast your computer can process the data, but rather how fast the data comes from the server. 这里的限制因素不是计算机处理数据的速度,而是数据来自服务器的速度。

Rather than try to speed this up using multiple threads, I would suggest that you create a WebClient rather than WebRequest . 我建议您创建一个WebClient而不是WebRequest ,而不是尝试使用多个线程加快速度。 You can then call WebClient.DownloadDataAsync to download data into memory in the background, or call WebClient.DownloadFileAsync to download directly to a file. 然后,您可以调用WebClient.DownloadDataAsync将数据下载到后台内存中,或者调用WebClient.DownloadFileAsync直接下载到文件中。

Neither one of those will make the download any faster, but they will prevent your user interface from being non-responsive during the download. 其中任何一个都不会使下载速度更快,但它们会阻止您的用户界面在下载过程中无响应。

Threads will not help you here; 线程在这里不会帮助你; you are going to be blocked on IO. 你将被阻止IO。 Rather than 1 thread blocked on IO, you will now have multiple threads blocked on IO. 而不是在IO上阻塞1个线程,您现在将在IO上阻止多个线程。 In fact, in many cases talking to the same resource (or parallel but related resources) on multiple threads will decrease IO throughput, plus the threading overheads. 实际上,在许多情况下,在多个线程上与相同的资源(或并行但相关的资源)进行通信会降低 IO吞吐量以及线程开销。 Lose : lose. 输了:输了。

Also - most streams are not designed for threading; 此外 - 大多数流不是为线程设计的; you would need some very complex co-ordination code to make sure you reassemble the stream in the right order and don't mess up the internal state; 你需要一些非常复杂的协调代码,以确保你按照正确的顺序重新组合流,不要搞乱内部状态; frankly, it isn't worth it. 坦白说,这不值得。

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

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