Public · Protected · Private
Different ways of starting a THREAD
Type: Public  |  Created: 2012-07-19  |  Frozen: Yes
« Previous Public Blog Next Public Blog »
Comments
  • //given Four methods //1 without args, 2With one Arg, 3With return value,4consuming work static void workHard() { for (int i = 0; i < 1000; i++) Console.Write("y"); } static void workHardArg(object h) { if (h == null) h = string.Empty; for (int i = 0; i < 1000; i++) { Console.Write(h.ToString()); } } static int WorkWithResult(string s) { return s.Length; } private string DownloadString(string p) { try {using (var wc = new System.Net.WebClient())return wc.DownloadString(p);} catch(Exception err){}return string.Empty; }
    2012-07-19 05:44
  • //1 Thread t = new Thread(workHard); t.Start(); //2 new Thread(workHard).Start(); //3 Thread t2 = new Thread(new ThreadStart(Form1.workHard)); t2.Start(); //4 Thread t3 = new Thread(() => { for (int i = 0; i < 1000; i++) Console.Write("y"); }); t3.Start(); //5 Thread t4 = new Thread(workHardArg); t4.Start("Hello from t4!"); //6 Thread t5 = new Thread(() => workHardArg("t5")); t5.Start(); //7 Thread t6 = new Thread(delegate() { workHardArg("test"); }); t6.Start(); //8 Thread t10 = new Thread(workHardArg) { Name = "obedient", IsBackground = true, Priority = ThreadPriority.Lowest }; t10.Start("Hello from t!"); //9 Task.Factory.StartNew(workHard); //10 ThreadPool.QueueUserWorkItem(workHardArg); ThreadPool.QueueUserWorkItem(workHardArg, 123); //11 Task<string> task = Task.Factory.StartNew<string>(() => DownloadString("http://www.linqpad.net")); string result = task.Result; MessageBox.Show(result); //12 for (int i = 0; i < 10; i++) new Thread(() => Console.Write(i)).Start(); //13 Func<string, int> method = WorkWithResult; IAsyncResult cookie = method.BeginInvoke("test", null, null); int result5 = method.EndInvoke(cookie); Console.WriteLine("String length is: " + result);
    2012-07-19 05:48
This blog is frozen. No new comments or edits allowed.