static void Main(string[] args) { ThreadSample sample = new ThreadSample(); ThreadStart tsa = new ThreadStart(sample.ThreadFuncA); ThreadStart tsb = new ThreadStart(sample.ThreadFuncB); Thread ta = new Thread(tsa); Thread tb = new Thread(tsb); // A 线程的 优先级 高于 B 线程 // 在线程代码的 计数 中,A线程的 数值要大于 B线程。 ta.Priority = ThreadPriority.Highest; tb.Priority = ThreadPriority.Lowest; // 启动. ta.Start(); tb.Start(); // 执行5秒以后, 结束 Thread.Sleep(5000); ta.Abort(); tb.Abort(); Console.ReadLine(); }
static void Main(string[] args) { var sample = new ThreadSample(10); var threadOne = new Thread(sample.CountNumbers); threadOne.Name = "ThreadOne"; threadOne.Start(); threadOne.Join(); WriteLine("----------------------------------"); var threadTwo = new Thread(Count); threadTwo.Name = "ThreadTwo"; threadTwo.Start(8); threadTwo.Join(); WriteLine("----------------------------------"); var threadThree = new Thread(() => CountNumbers(12)); threadThree.Name = "ThreadThree"; threadThree.Start(); threadThree.Join(); WriteLine("----------------------------------"); int i = 10; var threadFour = new Thread(() => PrintNumber(i)); i = 20; var threadFive = new Thread(() => PrintNumber(i)); threadFour.Start(); threadFive.Start(); }
static void Main2(string[] args) { //Thread t = new Thread(childFn); //t.Start(); //t.Join(); ThreadSample sample = new ThreadSample(); Thread producer = new Thread(sample.Producer); //Thread producer = new Thread(() => // { // while (true) // { // //Thread.Sleep(1000); // sample.Producer(); // } // }); Thread consumer = new Thread(() => { while (true) { //Thread.Sleep(1000); sample.Consumer(); } }); producer.Start(); consumer.Start(); producer.Join(); consumer.Join(); Console.WriteLine("Hi Everyone"); Console.ReadLine(); }
private void button4_Click(object sender, EventArgs e) { var sampleBackground = new ThreadSample(5); var threadTwo = new Thread(sampleBackground.CountNumbers); threadTwo.IsBackground = true; threadTwo.Name = "后台线程"; threadTwo.Start(); }
static void Main(string[] args) { // 静态线程方法的. StaticThreadSample.StartThread(); // 普通线程方法的. ThreadSample.StartThread(); Console.WriteLine("按 CTRL+C 结束操作!"); }
private void button7_Click(object sender, EventArgs e) { var sampleBackground = new ThreadSample(10); var threadOne = new Thread(sampleBackground.CountNumbers); threadOne.Name = "线程"; threadOne.Start(); textBox1.Text += "结束\r\n"; }
private void button5_Click(object sender, EventArgs e) { var sampleBackground = new ThreadSample(5); var threadTwo = new Thread(sampleBackground.CountNumbers); threadTwo.Name = "子线程"; threadTwo.Start(); threadTwo.Join(); t.Text += "子线程计数完成\r\n"; }
private void button6_Click(object sender, EventArgs e) { var sampleBackground = new ThreadSample(5); var threadOne = new Thread(sampleBackground.muPlus); var threadTwo = new Thread(sampleBackground.muPlus); threadOne.Name = "线程1"; threadTwo.Name = "线程2"; threadOne.Start(); threadTwo.Start(); }
private void button3_Click(object sender, EventArgs e) { var sampleForeground = new ThreadSample(5); var threadOne = new Thread(sampleForeground.CountNumbers); threadOne.Name = "前台线程"; threadOne.Start(); }
static void Main(string[] args) { var sampleForeground = new ThreadSample(10); var sampleBackground = new ThreadSample(20); var threadOne = new Thread(sampleForeground.CountNumbers); threadOne.Name = "ForegroundThread"; var threadTwo = new Thread(sampleBackground.CountNumbers); threadTwo.Name = "BackgroundThread"; threadTwo.IsBackground = true; threadOne.Start(); threadTwo.Start(); }
private void button11_Click(object sender, EventArgs e) { var sampleBackground = new ThreadSample(10); AsyncCallback callback = p => { t.Text += ($"到这里已经完成了。{Thread.CurrentThread.ManagedThreadId.ToString("00")}。"); }; //异步调用回调 Action fn = sampleBackground.CountNumbers; for (int i = 0; i < 5; i++) { string name = string.Format($"btnSync_Click_{i}"); fn.BeginInvoke(callback, null); } }
//class ThreadSample //{ // private bool _isStopped = false; // public void Stop() // { // _isStopped = true; // } // public void CountNumbers() // { // long counter = 0; // while (!_isStopped) // { // counter++; // } // Console.WriteLine("{0} with {1,11} priority " + // "has a count = {2,13}", Thread.CurrentThread.Name, // Thread.CurrentThread.Priority, // counter.ToString("N0")); // } #endregion #region 前后台线程 private void button4_Click(object sender, EventArgs e) { var sampleForeground = new ThreadSample(10); var sampleBackground = new ThreadSample(20); var threadOne = new Thread(sampleForeground.CountNumbers); threadOne.Name = "ForegroundThread"; var threadTwo = new Thread(sampleBackground.CountNumbers); threadTwo.Name = "BackgroundThread"; threadTwo.IsBackground = true; //前台线程执行完 进程会关闭 threadOne.Start(); threadTwo.Start(); }
static void RunThreads() { var sample = new ThreadSample(); var threadOne = new Thread(sample.CountNumbers); threadOne.Name = "ThreadOne"; var threadTwo = new Thread(sample.CountNumbers); threadTwo.Name = "ThreadTwo"; threadOne.Priority = ThreadPriority.Highest; threadTwo.Priority = ThreadPriority.Lowest; threadOne.Start(); threadTwo.Start(); Thread.Sleep(TimeSpan.FromSeconds(2)); sample.Stop(); }
/// <summary> /// 运行线程 /// </summary> static void RunThreads() { var sample = new ThreadSample(); var t1 = new Thread(sample.CountNumbers); t1.Name = "Thread One"; var t2 = new Thread(sample.CountNumbers); t2.Name = "Thread Two"; t1.Priority = ThreadPriority.Highest;//设置线程的优先级 t2.Priority = ThreadPriority.Lowest; t1.Start(); t2.Start(); Thread.Sleep(TimeSpan.FromSeconds(2)); sample.Stop(); }
static void Main(string[] args) { ThreadSample sample = new ThreadSample(); ThreadStart ts = new ThreadStart(sample.ThreadFunc); Thread t = new Thread(ts); // 启动. t.Start(); // 线程启动10秒以后,终止执行 Thread.Sleep(10000); t.Abort(); Console.ReadLine(); }
public static void ThreadRun() { ThreadSample sample = new ThreadSample(); Thread t1 = new Thread(sample.CountNumber); Thread t2 = new Thread(sample.CountNumber); t1.Priority = ThreadPriority.Highest; t2.Priority = ThreadPriority.Lowest; t1.Name = "ThreadOne"; t2.Name = "ThreadTwo"; t1.Start(); t2.Start(); Sleep(TimeSpan.FromSeconds(2)); sample.Stop(); }
static void Main(string[] args) { // 通过类字段传递 var sample = new ThreadSample(10); var threadOne = new Thread(sample.CountNumbers); threadOne.Name = "ThreadOne"; threadOne.Start(); threadOne.Join(); Console.WriteLine("--------------------------"); // Start传递 var threadTwo = new Thread(Count); threadTwo.Name = "ThreadTwo"; threadTwo.Start(8); threadTwo.Join(); Console.WriteLine("--------------------------"); // Lambda表达式 var threadThree = new Thread(() => CountNumbers(12)); threadThree.Name = "ThreadThree"; threadThree.Start(); threadThree.Join(); Console.WriteLine("--------------------------"); int i = 10; var threadFour = new Thread(() => PrintNumber(i)); i = 20; var threadFive = new Thread(() => PrintNumber(i)); threadFour.Start(); threadFive.Start(); Console.ReadKey(); }
public static void Test() { ThreadSample sample = new ThreadSample(); Thread t1 = new Thread(sample.CountNumbers); t1.Name = "Thread First"; Thread t2 = new Thread(sample.CountNumbers); t2.Name = "Thread Second"; t1.Priority = ThreadPriority.Highest; t2.Priority = ThreadPriority.Lowest; t1.Start(); t2.Start(); Thread.Sleep(TimeSpan.FromSeconds(2)); sample.Stop(); }
static void Main(string[] args) { Console.WriteLine("Escolha uma opção"); Console.WriteLine("1-Delegagte"); Console.WriteLine("2-Lock"); Console.WriteLine("3-SocketTCP"); Console.WriteLine("4-Task"); Console.WriteLine("5-Threads"); Console.WriteLine("5-Collections"); var key = Console.ReadLine(); var opt = int.TryParse(key, out int result) ? result : 0; Console.WriteLine("\n"); switch (opt) { case 1: // Creates one delegate for each method. For the instance method, an // instance (mySC) must be supplied. For the static method, use the // class name. mySampleClass mySC = new mySampleClass(); myMethodDelegate myD1 = new myMethodDelegate(mySC.myStringMethod); myMethodDelegate myD2 = new myMethodDelegate(mySampleClass.mySignMethod); // Invokes the delegates. Console.WriteLine("{0} is {1}; use the sign \"{2}\".", 5, myD1(5), myD2(5)); Console.WriteLine("{0} is {1}; use the sign \"{2}\".", -3, myD1(-3), myD2(-3)); Console.WriteLine("{0} is {1}; use the sign \"{2}\".", 0, myD1(0), myD2(0)); break; case 2: Task.Run(async() => await AccountTest.MainSample()).Wait(); break; case 3: SynchronousSocketClient.StartClient(); break; case 4: TaskSample.MainSample(); break; case 5: ThreadSample.Run(); break; case 6: Console.WriteLine("Collections"); Console.WriteLine("1-ConcurrentDictionary"); Task.Run(async() => await TestDictionary.RunSample()).Wait(); break; default: Console.WriteLine("Opção inválida"); break; } }