.NET ez 2012-06-04
基本的Thread寫法
using System; using System.Threading; namespace Test { class Program { static void Main(string[] args) { Thread t = new Thread(new ThreadStart(ThreadFun)); t.Start(); } static void ThreadFun() { int x = 0; while (++x > 0) Console.WriteLine(x.ToString()); } } }
將變數帶入Thread
using System; using System.Threading; namespace Test { class Program { static void Main(string[] args) { Thread t = new Thread(new ParameterizedThreadStart(ThreadFun)); t.Start(2); } static void ThreadFun(object i) { int x = 0; int y = (int)i; while (++x > 0) Console.WriteLine(((++x) * y).ToString()); } } }
簡單的Thread產生方式
using System; using System.Threading; namespace Test { class Program { static void Main(string[] args) { new Thread(() => { int x = 0; while (++x > 0) Console.WriteLine(x.ToString()); }).Start(); } } }
簡單的Thread產生方式,並且帶入變數
using System; using System.Threading; namespace Test { class Program { static void Main(string[] args) { new Thread((object i) => { int x = 0; int y = (int)i; while (++x > 0) Console.WriteLine(((++x) * y).ToString()); }).Start(2); } } }
標籤: .NET