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 poolWaitCallback 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/>//.....
}