I believe everyone has used the experience of downloading resources such as Internet Express, which can be used to set the number of threads (in recent years, the default version is 10, and the default value is 5 ). It will divide the file into the same part as the number of threads, and then each thread downloads its own part, which may improve the download efficiency. I believe that many threads are added to improve the download efficiency. However, careful users will find that, when the bandwidth is fixed, the higher the thread, the faster the speed, but the peak value is reached at a certain point.
Multithreading in C # is not difficult to implement. It has a namespace: System. Threading, which supports multiple threads.
To enable a new thread, you must initialize the following:
Threadstart startdownload = new threadstart (download ); // Thread start setting: that is, each thread executes download (). Note: Download () must be a method without parameters. Thread downloadthread = new thread (startdownload); // instantiate the new class to be enabled Downloadthread. Start (); // enable the thread |
Because the method started at the beginning of a thread cannot contain parameters, this makes it difficult to share resources with multiple threads. However, we can use Class-level variables (of course we can also use other methods. The author uses this method as the easiest way to use) to solve this problem. After you know how to enable multi-threaded download, you may have several questions:
1. How to manipulate the number of threads?
2. How to prevent multiple threads from downloading the same webpage?
3. How to Determine the end of a thread?
4. How to manipulate the end of a thread?
Below are some solutions to these problems:
1. We can implement the number of threads through the for loop, just like the hitting program for beginners.
For example, if you know that you have specified n (an int variable) threads, you can use the following method to enable five threads.
Thread [] downloadThread; // The famous download thread. This is the advantage of C #, that is, the length of the array does not need to be specified during initialization. It can be specified only during use. This should be a class level, which makes it possible for other method controls. ThreadStart startDownload = new ThreadStart (DownLoad ); // Thread start setting: that is, each thread executes DownLoad () DownloadThread = new Thread [n]; // apply for resources for the Thread and determine the total number of threads For (int I = 0; I <n; I ++) // enables the specified number of threads {
DownloadThread [I] = new Thread (startDownload); // specifies the Thread start setting.
DownloadThread [I]. Start (); // enable threads one by one
} |
Okay. Is it easy to implement control on the number of threads enabled?
2. the following problem occurs: All threads call the DonwLoad () method, so how can they avoid downloading the same web page at the same time?
This problem can be solved by setting up the Url address table. Each address in the table can only be applied by one thread. Specific implementation:
You can use the database to create a table with four columns in the table, one of which is used to store the URL address. The other two columns respectively store the thread corresponding to the address and the number of times the address is applied, the last column stores the downloaded content. (Of course, the corresponding thread column is not necessary ). After a thread applies, the corresponding thread column is set to the current thread number, and whether a column has been applied for is set to apply once. In this way, other threads cannot apply for this page. If the download is successful, the content is saved to the content column. If it fails, the content column is still blank, which serves as one of the basis for whether to re-download. If it fails to be repeated, the process will reach the number of retries (the number of times the address should be requested, which can be set by the user) then, apply for the next URL address. Well, this solves thread conflicts in multiple threads. Of course, the de-duplication problem can also be solved in the C # language. Only a temporary file (text) is created at the root to save all URL addresses, you can set corresponding properties for them, but the search efficiency may be less efficient than that of the database.
3. It is difficult to judge the end of a thread because it is always searching for new links. Assume that the thread has not been able to apply for a new URL after N times of repetition, so it may feel that it has downloaded all the links. The main code is as follows:
String url = "";
Int times = 0;
While (url = "") // if no matching record is found, keep searching for matching records
{
Url = getUrl. GetAUrl (...... ); // Call the GetAUrl method to obtain a url Value
If (url = "") // if no
{
Times ++; // The number of attempts increases.
Continue; // perform the next attempt
} If (times> N) // if the number of attempts has been reached, exit the process.
{
DownloadThread [I]. Abort; // exit the process
} Else // if no attempts are made
{
Times = 0; // The number of attempts to return to zero
}
// Process the Url.
} |
4. This problem is relatively simple, because it has been proposed in Issue 1 to name the thread as a class-level array, which makes it easy to manipulate. You can end with a for loop. The Code is as follows:
For (int I = 0; I <n; I ++) // closes the specified number of n threads.
{
DownloadThread [I]. Abort (); // close threads one by one
} |
Well, a multi-threaded C # program is completed in this way. In front of C #, its implementation is so simple.
Article by: http://www.cnblogs.com/luluping/archive/2009/09/28/1575716.html