惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园 - 聂微东
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
The GitHub Blog
The GitHub Blog
博客园_首页
博客园 - 叶小钗
腾讯CDC
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
D
Docker

博客园 - 左佩玉

通过了 70-316 考试 .NET SDK 工具 进程 程序配置 程序集 全局化和本地化 实现帮助 可访问性设计 访问和调用外部组件 实现打印 自定义控件 使用 GDI+ 在ADO.net 中使用xml 绑定、查看和筛选数据 ADO.net 处理和抛出异常 使用Debug和Trace 测试和调试 继承 System.Collections.CollectionBase 创建一个强类型集合类
线程
左佩玉 · 2005-08-27 · via 博客园 - 左佩玉

System.Threading

创建线程代理
ThreadStart threadFunc;
MyClass myObject = new MyClass();
threadFunc = new ThreadStart(myObject.Func1);

创建线程对象
Thread thread1 = new Thread(threadFunc);

合并
MyClass myObject = new MyClass();
Thread thread1 = new Thread(new ThreadStart( myObject.Func1 ));

启动线程
thread1.Start();

使主线程睡眠
Thread.Sleep(1000);

挂起
thread1.Suspend();

恢复
thread1.Resume();

终止线程
thread1.Abort();

使调用线程一直处于等待状态直到被调用线程结束
thread1.Join();
设置等待时间
  if  (thread1.Join(1000) == true)
  {
     //被等待线程已结束
   }
   else
  {
     //被等待线程没结束
   }   

线程之间的同步
Monitor.Enter(myObject);
....//可在此用 Monitor.Pulse(myObject); 释放指定对象上的同步锁,转入就绪队列 
    //Monitor.PulseAll(myObject);
Monitor.Exit(myObject);

指定时间获取同步锁,不会被堵塞
Monitor.TryEnter(myObject, 10000);

Monitor.TryEnter(myObject, Timeout.Infinite);

释放同步锁,线程进入等待
Monitor.Wait(myObject);
Monitor.Wait(myObject,1000);