[C # multi-thread processing series Topic 8-multithreading supplement

Source: Internet
Author: User

Some people may wonder, what is the role of so many threads in practical applications? Here I use multiple threads to download an object.

Server Page:

 

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "default. aspx. cs" inherits = "fileserver. Default" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

The server page is just a simple display of some information about the file to be downloaded. Here we use handler. ashx is used to download files and write the files into binary bytes to the output stream. The specific implementation code is as follows:

 

 

Public void download (object state) {// timer object stopwatch Sw = stopwatch. startnew (); httpwebrequest request; httpwebresponse response; stream; // The download address string savepath = "d :\\ download#"; filestream savestream = new filestream (savepath, filemode. openorcreate); try {// send request = (httpwebrequest) httpwebrequest. create (URL); // get response object response = (httpwebresponse) request. getresponse (); // get response stream = response. getresponsestream (); byte [] bytes = new byte [10240]; int readsize; // 10240 bytes are read each time. // the synchronous reading method is used. // the time consumed for calculation. readsize = stream. read (bytes, 0, bytes. length); While (readsize> 0) {savestream. write (bytes, 0, readsize); readsize = stream. read (bytes, 0, bytes. length);} SW. stop (); MessageBox. show ("download time:" + SW. elapsed. tostring (), "prompt");} catch (exception ex) {MessageBox. show (ex. message, "error");} finally {savestream. close ();}}

 

In this way, the thread pool thread is used to simply download files from the client on the server, and the thread pool thread is not used as the main thread. As a result, the interface can be operated during file download, if multithreading is not used, the interface will be "stuck" during the download process, which will bring a poor user experience.

 

In fact, I still want to implement the complex functions. The functions I want to implement are resumable data transfer on the server side and multi‑thread download on the client side, in this example, only one thread pool thread is used to complete the download task. In this example, multiple thread pool threads are used to complete the download task. Each thread is only responsible for reading part of the task, then, the bytes read in each thread are merged into the complete file bytes. However, there is a problem here: How to Implement the Resume function on the server side, the client sends partial read requests through the addrange method, and the server will parse the request header range. I still know the implementation principle, but there is still a problem during the process. Therefore, we can only share with you a simple file download function. As for multi-threaded download, resumable upload, and upload of large files, I will share it with you after learning, if Daniel can help me solve the problem of resumable data transfer on the server, leave a message.

Source File Download link: http://files.cnblogs.com/zhili/FileServer.zip (after downloading, you only need to add a file under the server-side resources folder to run)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.