Interesting multithreaded Programming (2)--parameter passing in a thread

Source: Internet
Author: User
Programming | multithreading
You can pass parameters to a thread by using a class, a method of a class, or a property of a class:
public class urldownloader{ string URL; Public urldownloader (string url) { this.url = URL; } public void Download () {
WebClient WC = new WebClient ();
Console.WriteLine ("Downloading" + URL); byte[] buffer = WC. Downloaddata (URL); string download = Encoding.ASCII.GetString (buffer); Console.WriteLine (download); Console.WriteLine ("Download successful.");
Here you can save the download for processing ... } [... Use them in another class ...] Urldownloader Downloader = new Urldownloader (yoururl); new Thread (New ThreadStart (downloader. Download)). Start ();
Note how the parameters are passed.
You can also do this in. NET 2.0:
(code-beside)
Mode one:
ThreadStart starter = delegate {Download (yoururl);}); New Thread (starter). Start ();
Using a thread pool
WaitCallback callback = Delegate (object state) {Download ((string) state); ThreadPool.QueueUserWorkItem (callback, Yoururl);
Mode two (using Parameterizedthreadstart):
Thread t = new Thread (new Parameterizedthreadstart (DownloadURL)); T.start (myurl); static void DownloadURL (object URL) { c0/>//.....
}


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.