C # implements Multithreading

Source: Internet
Author: User

Multithreading is not difficult to implement in C #. It has a namespace: System.Threading, which provides multi-threaded support.

To open a new thread, the following initialization is required:

ThreadStart startdownload = new ThreadStart (DownLoad); Thread start settings: That is, each thread executes DownLoad (), note: DownLoad () must be a method without parameters

Thread downloadthread = new Thread (startdownload); Instantiate a new class to open

Downloadthread.start ();//Open thread

Because methods that start at the start of a thread cannot have parameters, this adds trouble to multithreaded shared resources. However, we can use class-level variables (and, of course, other methods, which I think are the easiest to use) to solve this problem. Once you know how to turn on multi-threaded downloads, you may have several questions:

1. How do I control the number of threads?

2. How do I prevent multiple threads from downloading the same page?

3. How to determine the end of a thread?

4. How do I control thread end?

Here are some solutions to these problems:

1. Number of threads we can do this with a for loop, just as we did when we were beginning to program.

For example, a known user specified n (it is an int variable) thread bar, you can open five threads in the following way

thread[] downloadthread;//The download thread, which is the advantage of C #, that when an array is initialized, it does not need to specify its length and can be specified when it is used. This reputation should be class-level, which provides the possibility for other methods to control them

ThreadStart startdownload = new ThreadStart (DownLoad);//thread Start setting: That is, each thread executes DownLoad ()

Downloadthread = new thread[n];//request resources for thread to determine the total number of threads

for (int i = 0; i < n; i++)//Open the specified number of threads

{

Downloadthread[i] = new Thread (startdownload);//Specify Thread start setting

Downloadthread[i]. Start ();//Turn on threads individually

}

Okay, is it easy to control the number of threads that are turned on?

2. One problem follows: All threads call the Donwload () method, so how do you avoid them downloading the same page at the same time?

This problem is also resolved, as long as the URL Address table is established, each address in the table is only allowed by a thread to apply. Specific implementation:

The database can be used to create a table with four columns, one of which is dedicated to storing the URL address, the other two columns hold the corresponding thread for the address and the number of times the address is requested, and the last column holds the downloaded content. (Of course, a column of corresponding threads is not necessary.) When the thread request is applied, the corresponding threads column is set to the current thread number and the request is set to apply once, so that other threads cannot request the page. If the download succeeds, the content is saved to the Content column. If unsuccessful, the content column is still empty, as one of the basis for downloading again, and if it is unsuccessful repeatedly, the process will request the next URL address after reaching the retry count (the number of times the address should be requested and the user can set it). The main code is as follows (in VFP, for example):

< set up Tables >

CREATE TABLE (Ctablename) (Curl m, Ctext m, ldowned I, threadnum i) && create a table ctablename.dbf with address, text content, number of attempts to download, Thread flag (initial value is-1, thread flag is an integer starting from 0) four fields

< extract URL address >

Cfullname = (ctablename) + '. dbf ' && add extension to table

Use (Cfullname)

GO TOP

LOCATE for (EMPTY (ALLTRIM (Ctext)) and ldowned < 2 and (threadnum = thisnum OR threadnum =-1)) && find not yet downloaded as The URL address that belongs to this thread's permissions, Thisnum is the number of the current thread, which can be passed by parameter

Goturl = Curl

Recnum = RECNO ()

If Recnum <= reccount () then && if the URL address is found in the list

Update (cfullname) SET ldowned = (ldowned + 1), threadnum = Thisnum WHERE RECNO () = recnum && Update table, updates this record to a request, i.e. download Number of times plus 1, the thread flag column is set to the number of this thread.

< download content >

Cfulltablename = (ctablename) + '. dbf '

Use (Cfulltablename)

SET EXACT on

LOCATE for curl = (Csiteurl) && Csiteurl is a parameter that corresponds to the URL address of the downloaded content

Recnumnow = RECNO () && get the record number containing this address

UPDATE (cfulltablename) SET Ctext = (ccontent) WHERE RECNO () = Recnumnow && Insert corresponding contents of corresponding address

< Insert new address >

Ctablename = (ctablename) + '. dbf '

Use (Ctablename)

GO TOP

SET EXACT on

LOCATE for curl = (Cnewurl) && Find There is no this address

If RECNO () > RecCount () then && if this address is not already available

SET CARRY OFF

INSERT into (ctablename) (Curl, Ctext, ldowned, Threadnum) VALUES ((Cnewurl), "", 0,-1) && Add a home address to the list

Well, this solves the thread conflict in multi-threading. Of course, the problem can also be solved in the C # language, only the root of the creation of a temporary file (text can), save all the URL address, the difference to set their corresponding properties, but the search efficiency may be less than the database faster.

3. Thread completion is difficult to judge because it is always looking for new links. The user thinks it can be assumed that the thread repeats n times and is not able to apply for a new URL address, so it can be assumed that it has downloaded all the links. The main code is as follows:

String url = "";

int times = 0;

while (url = = "")//If no qualifying records are found, keep looking for eligible records

{

url = Geturl.getaurl (...); /Call the Getaurl method to try to get a URL value

if (url = = "")//If not found

{

Times ++;//attempts to increment

Continue Make the next attempt

}

if (Times > N)//If the number of attempts has been attempted, exit the process

{

Downloadthread[i]. Abort; Exit process

}

else//If you haven't tried enough times

{

Times = 0; Number of attempts zeroed

}

Processing of the resulting URL for the next step

}

4. The problem is relatively simple, as it has been suggested in question one that the thread is known as a class of series, which is easy to control. Just use a For loop to end. The code is as follows:

for (int i = 0; i < n; i++)//Turn off the number of threads for the specified number of n

{

Downloadthread[i]. Abort ();//close thread one by one

}

Links with a friend

Http://www.tjjxnjy.com

Http://www.cdnjy.com

Http://www.beijingnjy.com

Http://www.cqnjy.com

Http://www.cztnjl.com

C # implements Multithreading

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.