经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C#开启线程的四种方式示例详解
来源:jb51  时间:2018/10/26 9:46:10  对本文有异议

一、异步委托开启线程

  1. public static void Main(string[] args){
  2. Action<int,int> a=add;
  3. a.BeginInvoke(3,4,null,null);//前两个是add方法的参数,后两个可以为空
  4. Console.WriteLine("main()");
  5. Console.ReadKey();
  6. }
  7. static void add(int a,int b){
  8. Console.WriteLine(a+b);
  9. }

运行结果:

如果不是开启线程,像平常一样调用的话,应该先输出7,再输出main()

二、通过thread类开启线程

  1. using System;
  2. using System.Threading;
  3. public static void Main(string[] args){
  4. Thread t=new Thread(DownLoadFile_My);//创建了线程还未开启
  5. t.Start("http://abc/def/**.mp4");//用来给函数传递参数,开启线程
  6. Console.WriteLine("main()");
  7. Console.ReadKey();
  8. }
  9. //thread开启线程要求:该方法参数只能有一个,且是object类型
  10. static void DownLoadFile_My(object filePath){
  11. Console.WriteLine("开始下载:"+filePath);
  12. Thread.Sleep(2000);
  13. Console.WriteLine("下载完成!");
  14. }

运行结果:

三、通过线程池开启线程

  1. public static void Main(string[] args){
  2. ThreadPool.QueueUserWorkItem(DownLoadFile_My);
  3. ThreadPool.QueueUserWorkItem(DownLoadFile_My);
  4. ThreadPool.QueueUserWorkItem(DownLoadFile_My);
  5. ThreadPool.QueueUserWorkItem(DownLoadFile_My);
  6. ThreadPool.QueueUserWorkItem(DownLoadFile_My);
  7. ThreadPool.QueueUserWorkItem(DownLoadFile_My);
  8. ThreadPool.QueueUserWorkItem(DownLoadFile_My);
  9. ThreadPool.QueueUserWorkItem(DownLoadFile_My);
  10. ThreadPool.QueueUserWorkItem(DownLoadFile_My);
  11. Console.WriteLine("main()");
  12. Console.ReadKey();
  13. }
  14. static void DownLoadFile_My(object state){
  15. Console.WriteLine("开始下载... 线程ID:"+Thread.CurrentThread.ManagedThreadId);
  16. Thread.Sleep(2000);
  17. Console.WriteLine("下载完成!");
  18. }

运行结果:

4、通过任务开启线程

1>Task开启线程

  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. public static void Main(string[] args){
  5. Task t=new Task(DownLoadFile_My);
  6. t.Start();
  7. Console.WriteLine("main()");
  8. Console.ReadKey();
  9. }
  10. static void DownLoadFile_My( ){
  11. Console.WriteLine("开始下载... 线程ID:"+Thread.CurrentThread.ManagedThreadId);
  12. Thread.Sleep(2000);
  13. Console.WriteLine("下载完成!");
  14. }

运行结果:

2>TaskFactory开启线程

  1. public static void Main(string[] args){
  2. TaskFactory tf=new TaskFactory();
  3. tf.StartNew(DownLoadFile_My);
  4. Console.WriteLine("main()");
  5. Console.ReadKey();
  6. }
  7. static void DownLoadFile_My( ){
  8. Console.WriteLine("开始下载... 线程ID:"+Thread.CurrentThread.ManagedThreadId);
  9. Thread.Sleep(2000);
  10. Console.WriteLine("下载完成!");
  11. }

运行结果:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对w3xue的支持。如果你想了解更多相关内容请查看下面相关链接

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号